#include"LEXSDL.h" #include int main(void){ // init sdl and sdl_image with default flags and check if error ocurres at initialization if(LEXSDL_Init(0)){ printf("SDL init failed\n"); return -1; } if(LEXSDL_InitIMG(0)){ printf("SDLIMG init failed\n"); return -1; } // create the window with title, default_flags and check if failed. if(LEXSDL_CreateWindow("LEXSDL Example - loadingRenderingTextures.c", 0) == NULL){ printf("failed to create SDL_Window\n"); return -1; } // create renderer with default flags and check if failed if(LEXSDL_CreateRenderer(0) == NULL){ printf("failed to create SDL_Renderer\n"); return -1; } // load the first texture // we are loading a file that doesn't exist for demonstration purposes int pixelDucksID = LEXSDL_TextureLoad("examples/assets/pixelDucksFAIL.png"); // since loading can fail we must check if it failed, negative numbers mean different errors (refer to the documentation), in this case the returned id should be -3 since it was not able to load the texture because the file doesn't exist. loadPixelDuckCheck: // ignore this, is for avoiding infinite loop in extreme case. switch(pixelDucksID){ case -1: printf("could not add the first texture.\nthis should not have happened\n"); return -1; break; case -2: printf("could not add texture.\nthis should not have happened\n"); return -1; break; case -3: // now load the correct file pixelDucksID = LEXSDL_TextureLoad("examples/assets/pixelDucks.png"); // extra statement to avoid infinite loop in case something is missing/wrong. // this should not be done in production static int avoidinfiniteloop = 0; avoidinfiniteloop++; if(avoidinfiniteloop > 1){ printf("\033[1;31mcould not load \"examples/assets/pixelDucks.png\".\nthis should not have happened\033[0m\n"); return -1; } goto loadPixelDuckCheck; break; default: printf("\"examples/assets/pixelDucks.png\" was loaded\n"); break; } // now start a new frame LEXSDL_NewFrame(); // render the texture LEXSDL_TextureDrawFill(pixelDucksID); // show the frame LEXSDL_ShowFrame(); // sleep for 3 seconds sleep(3); /* those are basic for creating a window and loading a texture */ // you can load textures included in the binary after converting them into its binary with tool like xxd // here is one included with the project #include"TopPatPng.h" int TopPatID = LEXSDL_TextureLoadBytes(TopPatPng, TopPatPngLen); // if you need more complex events it still nesceasary for you to handle them manually. /* Setup the events */ LEXSDL_SetupEvents(); /* a rendering/game loop */ // it will run until the SDL_QUIT event is triggered and recovered with LEXSDL_EventQuit() while(!LEXSDL_EventQuit()){ // Update/handle the events LEXSDL_HandleEvents(); // check for a keypress using scancodes if(LEXSDL_EventKey(SDL_SCANCODE_ESCAPE)) //set the quit event to true LEXSDL_EventDoQuit(); // a quit event can be canceled with LEXSDL_EventCancelQuit(); // start a frame. LEXSDL_NewFrame(); // you can render a texture at a position and size // don't forget that SDL `y` coordinate is top to bottom! LEXSDL_TextureDrawAt(TopPatID, 50,50,75,75); // and textures rotated LEXSDL_TextureDrawRotAt(pixelDucksID, 50+75,50,300,200,180); /* you can get the mouse position with different functions void LEXSDL_MousePos(int *x, int *y); int LEXSDL_MousePosX(void); int LEXSDL_MousePosY(void); also dont forget to update the mouse state! */ LEXSDL_MouseUpdate(); int txPos = LEXSDL_MousePosX(); int tyPos = LEXSDL_MousePosY(); static uint8_t trotdir = 0; static double trot = 0; /* and get if a click happened */ if(LEXSDL_MouseLeft()) trotdir = 0; if(LEXSDL_MouseRight()) trotdir = 1; if(trotdir) trot++; else trot--; // render a rotating texture at the mouse pos! LEXSDL_TextureDrawRotAt(TopPatID, txPos,tyPos, 50,50, trot); // show/render the frame. LEXSDL_ShowFrame(); } // and before exiting a cleanup. // Cleans the internal state and deallocate textures and varius things. LEXSDL_Terminate(); // Quits SDL and SDL_image. LEXSDL_Quit(); return 0; }