daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Jan 14th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include "SDL2/SDL.h"
  3. #include "SDL2/SDL_timer.h"
  4. #include "SDL2/SDL_image.h"
  5.  
  6. #define WINDOW_WIDTH (640)
  7. #define WINDOW_HEIGHT (480)
  8. #define SPEED (300)
  9.  
  10. int main (void)
  11. {
  12.     if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
  13.     {
  14.         printf("error initializing SDL: %s\n", SDL_GetError());
  15.         return 1;
  16.     }
  17.    
  18.     SDL_Window* win = SDL_CreateWindow("Hello There",
  19.                                         SDL_WINDOWPOS_CENTERED,
  20.                                         SDL_WINDOWPOS_CENTERED,
  21.                                         WINDOW_WIDTH, WINDOW_HEIGHT, 0);
  22.     if (!win)
  23.     {
  24.         printf("error creating window: %s\n", SDL_GetError());
  25.         SDL_Quit();
  26.         return 1;
  27.     }
  28.  
  29.     Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
  30.     SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
  31.     if(!rend)
  32.     {
  33.         printf("error creating renderer: %s\n", SDL_GetError());
  34.         SDL_DestroyWindow(win);
  35.         SDL_Quit();
  36.         return 1;
  37.     }
  38.  
  39.     SDL_Surface* surface = IMG_Load("~/Workspace/learnSDL/red.png");
  40.     if (!surface)
  41.     {
  42.         printf("error creating surface: %s\n", IMG_GetError());
  43.         SDL_DestroyRenderer(rend);
  44.         SDL_DestroyWindow(win);
  45.         SDL_Quit();
  46.         return 1;
  47.     }
  48.  
  49.     SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);
  50.     SDL_FreeSurface(surface);
  51.     if (!tex)
  52.     {
  53.         printf("error creating texture: %s\n", SDL_GetError());
  54.         SDL_DestroyRenderer(rend);
  55.         SDL_DestroyWindow(win);
  56.         SDL_Quit();
  57.         return 1;
  58.     }  
  59.  
  60.     SDL_Rect dest;
  61.     SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h);
  62.    
  63.     dest.w /= 4;
  64.     dest.h /= 4;
  65.    
  66.     float y_pos = (WINDOW_HEIGHT - dest.h) / 2;
  67.     float x_pos = (WINDOW_WIDTH - dest.h) / 2;
  68.     float x_vel = 0;
  69.     float y_vel = 0;   
  70.    
  71.     while (dest.y >= -dest.h)
  72.     {
  73.         SDL_RenderClear(rend);
  74.         dest.y = (int) y_pos;
  75.         SDL_RenderCopy(rend,tex, NULL, &dest);
  76.         SDL_RenderPresent(rend);
  77.        
  78.         y_pos -= (float) SPEED / 60;
  79.        
  80.         SDL_Delay(1000/60);
  81.     }
  82.    
  83.     SDL_RenderClear(rend);
  84.     SDL_RenderCopy(rend, tex, NULL, NULL);
  85.     SDL_RenderPresent(rend);
  86.  
  87.     SDL_Delay(5000);
  88.     SDL_DestroyTexture(tex);
  89.     SDL_DestroyRenderer(rend);
  90.     SDL_DestroyWindow(win);
  91.     SDL_Quit();
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top