VPN termux about China.net

Friday 16 August 2024

OpenGL_in_SDL_2😺

 

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include "SDL2/SDL.h"

#include "SDL_test_common.h"

#if defined(__IPHONEOS__) || defined(__ANDROID__)

#define HAVE_OPENGLES

#endif

#include "SDL_opengles.h"

struct mouse_handle {

    int x = 1;

    int y = 1;

} mouse;

 SDLTest_CommonState *state;

SDL_Window *win = NULL;

 SDL_Renderer* renderer= SDL_CreateRenderer(win, -1, SDL_WINDOW_OPENGL);

  SDL_Event *event;


static SDL_GLContext *context;


struct obj {

    float x;

    float y;

    float z;


    int Render() {

        static GLubyte color[3][4] = { {255, 0, 0, 255}, {0, 255, 0, 255}, {0, 0, 255, 255} };

        static GLfloat vertices[3][3] = { {0.0f, 0.5f, 0.0f}, {-0.5f, -0.5f, 0.0f}, {0.5f, -0.5f, 0.0f} };

        static GLubyte indices[3] = {2, 1, 0}; // Corrected the indices


        glPushMatrix(); // Save the current transformation matrix


        glTranslatef(x, y, z);

        glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);


        glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

        glEnableClientState(GL_COLOR_ARRAY);

        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glEnableClientState(GL_VERTEX_ARRAY);

        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, indices);


        glDisableClientState(GL_VERTEX_ARRAY);

        glDisableClientState(GL_COLOR_ARRAY);


        glPopMatrix(); // Restore the previous transformation matrix


        return 1;

    }

};

int main(int argc, char *argv[])

{

 SDL_DisplayMode mode;


   SDL_GL_MakeCurrent(win,context);

 state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);


 SDLTest_CommonInit(state); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);

bool sdlmainloop=true;

    bool running = true;

    while (running) {

        SDL_Event event;

        while (SDL_PollEvent(&event)) {

            if (event.type == SDL_QUIT) {

                running = false;

            }

              if (event.type == SDL_MOUSEMOTION)

  {

   mouse.x = event.motion.x;

   mouse.y = event.motion.y;

 

   

  }

  SDL_GL_CreateContext(*state->windows); 


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        obj gg;

        gg.x = 0.2f;

        gg.y = 0.4f;

        gg.z = 0.6f;

        gg.Render();


        obj gg1;

        gg1.x = 0.9f;

        gg1.y = 0.7f;

        gg1.z = 0.3f;

        gg1.Render();            

   SDL_GL_SwapWindow(*state->windows);


        }

}


 return 0;

}


No comments:

Post a Comment