tungggg

Texture_and_Surface

Feb 18th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <stdio.h>
  3. #include<iostream>
  4.  
  5. #include<windows.h>
  6. #include<string>
  7. #include<SDL_image.h>
  8. #define SCREEN_WIDTH 1280
  9. #define SCREEN_HEIGHT 720
  10.  
  11. // SDL_Window* g_windows = NULL;
  12. // SDL_Surface* g_screenSurface=NULL;
  13. // SDL_Surface* g_background= NULL;
  14. // load anh len man hinh window
  15. int main(int argc, char * agrv[]){
  16.  
  17.     // variable declare
  18.     SDL_Renderer * renderer =NULL;
  19.     SDL_Surface * tmp_surface= NULL;
  20.     SDL_Texture * texture=NULL;
  21.     bool is_running = true;
  22.     SDL_Window * window =NULL;
  23.     if ( SDL_Init(SDL_INIT_EVERYTHING)<0){
  24.         std::cout<<"Could not intialize SDL2"<<std::endl;
  25.         return 1;
  26.     }
  27.     else {
  28.         window =SDL_CreateWindow("Tungggg", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 400, SDL_WINDOW_SHOWN);
  29.     }
  30.     renderer=SDL_CreateRenderer(window, -1,0);
  31.     tmp_surface= IMG_Load("test.png");
  32.     if (tmp_surface!=NULL){
  33.         // get the information of the picture which was load into the surface
  34.         std::cout<<"width and height is "<<tmp_surface->w<<" "<<tmp_surface->h<<std::endl;
  35.     }
  36.     texture= SDL_CreateTextureFromSurface(renderer,tmp_surface);
  37.     SDL_FreeSurface ( tmp_surface);
  38.     SDL_SetRenderDrawColor(renderer,212,175,55,255);
  39.     SDL_RenderClear(renderer);
  40.     SDL_RenderCopy(renderer, texture, NULL,NULL);
  41.     SDL_RenderPresent(renderer);
  42.     while ( is_running ){
  43.         //  SDL_RenderClear(renderer);
  44.         SDL_Event event;
  45.         while ( SDL_PollEvent(&event)){
  46.             switch (event.type){
  47.                 case SDL_QUIT:{
  48.                     is_running=false;
  49.                     break;
  50.                 }
  51.                
  52.                
  53.             }
  54.             if (event.button.button== SDL_BUTTON_RIGHT){
  55.                 std::cout<<"right mouse is pressed"<<std::endl;
  56.             }
  57.             const Uint8*  state = SDL_GetKeyboardState(NULL);
  58.             if (state[SDL_SCANCODE_0]){
  59.                 std::cout<<"0 is pressed"<<std::endl;
  60.             }
  61.              if ( state[SDL_SCANCODE_0] &&  state[SDL_SCANCODE_1] && state[SDL_SCANCODE_2]){
  62.                 std::cout<<"1 and 2 is pressed"<<std::endl;
  63.             }
  64.         }
  65.         // SDL_RenderCopy(renderer,texture,NULL,NULL);
  66.         // SDL_RenderPresent(renderer);
  67.     }
  68.     SDL_DestroyWindow(window);
  69.     SDL_Quit();
  70.     return 0;
  71. }
Add Comment
Please, Sign In to add comment