SDL_DisplayMode displayMode;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){ std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; return 1; }
SDL_INIT_EVERYTHING
flag initializes all SDL subsystems. If you just need something specific, you can find a complete list on the wiki.SDL_GetDesktopDisplayMode(*int displayIndex, SDL_DisplayMode* mode)
.displayMode
object. int request = SDL_GetDesktopDisplayMode(0,&displayMode);
SDL_Window
class and call the function SDL_Window* SDL_CreateWindow(const char* title, int x, int y, int w, int h, Uint32 flags)
0,0
displayMode
to open the screen, we turn to the displayMode
object.SDL_WINDOW_FULLSCREEN
, and here I am inventing a bicycle with my DisplayMode
, but no!SDL_WINDOW_FULLSCREEN
avast shouted that they were trying to hack me. SDL_Window *win = SDL_CreateWindow("Hello World!", 0, 0, displayMode.w, displayMode.h, SDL_WINDOW_SHOWN); if (win == nullptr){ std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; return 1; }
SDL_Renderer* SDL_CreateRenderer(SDL_Window* window, int index, Uint32 flags)
-1
, then the render will use the first suitable driver.SDL_RENDERER_ACCELERATED
responsible for hardware acceleration and SDL_RENDERER_PRESENTVSYNC
responsible for vertical synchronization. SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == nullptr){ std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; return 1; }
SDL_Rect
class. SDL_Rect player_RECT; player_RECT.x = 0; // player_RECT.y = 0; // Y player_RECT.w = 333; // player_RECT.h = 227; // SDL_Rect background_RECT; background_RECT.x = 0; background_RECT.y = 0; background_RECT.w = displayMode.w; background_RECT.h = displayMode.h;
const int player_WIGHT = 333; // const int player_HEIGH = 227; // double TESTtexture_SCALE = 1.0; //
SDL_Texture
, SDL_Surface
, SDL_Rect
, SDL_Render
.SDL_Surface
- working with SDL_mixer.h
you can forget about it. But it’s silly to work with something without having any idea how it works.SDL_Texture
- SDL_Texture
creating an object of the SDL_Surface
structure, we have to turn it into a texture so that the render can do it. SDL_RenderCopy(SDL_Renderer* renderer, SDL_Texture* texture, const SDL_Rect* srcrect, const SDL_Rect* dstrect) SDL_RenderPresent(SDL_Renderer* renderer)
SDL_mixer.h
and SDL_Image.h
, so do not SDL_Texture *player = IMG_LoadTexture(ren,"..\\res\\player.png");
SDL_Surface *BMP_background = SDL_LoadBMP("..\\res\\background.bmp"); if (BMP_background == nullptr){ std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl; return 1; } SDL_Texture *background = SDL_CreateTextureFromSurface(ren, BMP_background); SDL_FreeSurface(BMP_background); // if (player == nullptr){ std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl; return 1; }
SDL_RenderClear(ren); // SDL_RenderCopy(ren,background,NULL,&background_RECT); // SDL_RenderCopy(ren, player, NULL, &player_RECT); // SDL_RenderPresent(ren); //!!
SDL_Event event; bool quit = false; while(!quit) while(SDL_PollEvent(&event)) { SDL_PumpEvents(); // . }
SDL_Texture* ARRAY_textures[2] = {background, player}; SDL_Rect* ARRAY_rect[2] = {&background_RECT, &player_RECT}; int ARRAY_texturesState[2] = {1,1};
if(event.type == SDL_QUIT) quit=true; if(event.type == SDL_MOUSEBUTTONDOWN) { if(event.button.button == SDL_BUTTON_LEFT && event.button.x <=10 && event.button.y <=10) quit = true; if(event.button.button == SDL_BUTTON_RIGHT) ARRAY_texturesState[1] = 1; if((event.button.button == SDL_BUTTON_LEFT) && (event.button.x >= player_RECT.x) && (event.button.y >= player_RECT.y) && (event.button.x <= player_RECT.w + player_RECT.x) && (event.button.y <= player_RECT.h + player_RECT.y)) ARRAY_texturesState[1] = 0; }
event.button.button
waiting for a special SDL flag that you can easily find on the wiki, and event.type
waiting for a flag about the type of event , a complete list of which is all there! const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
void move_UP (SDL_Renderer* render, SDL_Texture* texture, SDL_Rect &destrect, int offset = 5) { destrect.y -= offset; SDL_RenderClear(render); SDL_RenderCopy(render, texture,NULL,&destrect); } void move_DOWN (SDL_Renderer* render, SDL_Texture* texture, SDL_Rect &destrect, int offset = 5) { destrect.y += offset; SDL_RenderClear(render); SDL_RenderCopy(render, texture,NULL,&destrect); } void move_LEFT (SDL_Renderer* render, SDL_Texture* texture, SDL_Rect &destrect, int offset = 5) { destrect.x -= offset; SDL_RenderClear(render); SDL_RenderCopy(render, texture,NULL,&destrect); } void move_RIGHT(SDL_Renderer* render, SDL_Texture* texture, SDL_Rect &destrect, int offset = 5) { destrect.x += offset; SDL_RenderClear(render); SDL_RenderCopy(render, texture,NULL,&destrect); } void render_UPDATE(SDL_Renderer* render, SDL_Texture* texture[], SDL_Rect* destrect[], int states[]) { SDL_RenderClear(render); if(states[0]) SDL_RenderCopy(render, texture[0],NULL,destrect[0]); if(states[1]) SDL_RenderCopy(render, texture[1],NULL,destrect[1]); }
if((keyboardState[SDL_SCANCODE_UP])||(keyboardState[SDL_SCANCODE_W])) move_UP(ren,player,player_RECT); if((keyboardState[SDL_SCANCODE_DOWN])||(keyboardState[SDL_SCANCODE_S])) move_DOWN(ren,player,player_RECT); if((keyboardState[SDL_SCANCODE_LEFT])||(keyboardState[SDL_SCANCODE_A])) move_LEFT(ren,player,player_RECT); if((keyboardState[SDL_SCANCODE_RIGHT])||(keyboardState[SDL_SCANCODE_D])) move_RIGHT(ren,player,player_RECT); //ZOOM---------------------------------------------------------------- if(keyboardState[SDL_SCANCODE_KP_PLUS]) { TESTtexture_SCALE += 0.02; player_RECT.h = player_HEIGH * TESTtexture_SCALE; player_RECT.w = player_WIGHT * TESTtexture_SCALE; } if(keyboardState[SDL_SCANCODE_KP_MINUS]) { TESTtexture_SCALE -= 0.02; player_RECT.h = player_HEIGH * TESTtexture_SCALE; player_RECT.w = player_WIGHT * TESTtexture_SCALE; }
keyboardState[flag]
.true
if the button is pressed and false
in a callback. render_UPDATE(ren, ARRAY_textures, ARRAY_rect, ARRAY_texturesState); // SDL_RenderPresent(ren);
SDL_DestroyTexture(player); SDL_DestroyTexture(background);
SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); return 1;
Source: https://habr.com/ru/post/201392/
All Articles