Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "SDL2/SDL.h"
- #include "SDL2/SDL_timer.h"
- #include "SDL2/SDL_image.h"
- #define WINDOW_WIDTH (640)
- #define WINDOW_HEIGHT (480)
- #define SPEED (300)
- int main (void)
- {
- if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
- {
- printf("error initializing SDL: %s\n", SDL_GetError());
- return 1;
- }
- SDL_Window* win = SDL_CreateWindow("Hello There",
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- WINDOW_WIDTH, WINDOW_HEIGHT, 0);
- if (!win)
- {
- printf("error creating window: %s\n", SDL_GetError());
- SDL_Quit();
- return 1;
- }
- Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
- SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
- if(!rend)
- {
- printf("error creating renderer: %s\n", SDL_GetError());
- SDL_DestroyWindow(win);
- SDL_Quit();
- return 1;
- }
- SDL_Surface* surface = IMG_Load("~/Workspace/learnSDL/red.png");
- if (!surface)
- {
- printf("error creating surface: %s\n", IMG_GetError());
- SDL_DestroyRenderer(rend);
- SDL_DestroyWindow(win);
- SDL_Quit();
- return 1;
- }
- SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);
- SDL_FreeSurface(surface);
- if (!tex)
- {
- printf("error creating texture: %s\n", SDL_GetError());
- SDL_DestroyRenderer(rend);
- SDL_DestroyWindow(win);
- SDL_Quit();
- return 1;
- }
- SDL_Rect dest;
- SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h);
- dest.w /= 4;
- dest.h /= 4;
- float y_pos = (WINDOW_HEIGHT - dest.h) / 2;
- float x_pos = (WINDOW_WIDTH - dest.h) / 2;
- float x_vel = 0;
- float y_vel = 0;
- while (dest.y >= -dest.h)
- {
- SDL_RenderClear(rend);
- dest.y = (int) y_pos;
- SDL_RenderCopy(rend,tex, NULL, &dest);
- SDL_RenderPresent(rend);
- y_pos -= (float) SPEED / 60;
- SDL_Delay(1000/60);
- }
- SDL_RenderClear(rend);
- SDL_RenderCopy(rend, tex, NULL, NULL);
- SDL_RenderPresent(rend);
- SDL_Delay(5000);
- SDL_DestroyTexture(tex);
- SDL_DestroyRenderer(rend);
- SDL_DestroyWindow(win);
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement