Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SDL.h>
- // Screen dimension constants
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 480;
- enum KeySurface {
- KEY_SURFACE_DEFAULT,
- KEY_SURFACE_UP,
- KEY_SURFACE_DOWN,
- KEY_SURFACE_LEFT,
- KEY_SURFACE_RIGHT,
- KEY_SURFACE_TOTAL
- };
- bool init(SDL_Window **, SDL_Surface **);
- bool loadMedia(SDL_Surface **);
- SDL_Surface* loadSurface(std::string);
- bool close(SDL_Window **, SDL_Surface **);
- int main(int argc, char* args[]) {
- SDL_Window* window = NULL;
- SDL_Surface* surface = NULL;
- SDL_Surface** image = new SDL_Surface*[KEY_SURFACE_TOTAL];
- std::cout << "WHILE LOOP\n";
- if (!init(&window, &surface))
- std::cout << "Error during init: " << SDL_GetError() << std::endl;
- else {
- if (!loadMedia(image))
- std::cout << "Error during loadMedia: " << SDL_GetError() << std::endl;
- else {
- bool quit = false;
- SDL_Event e;
- SDL_Surface *current = image[KEY_SURFACE_DEFAULT];
- while (!quit) {
- while((SDL_PollEvent(&e)) != 0) {
- if (e.type == SDL_QUIT)
- quit = true;
- else if (e.type == SDL_KEYDOWN) {
- switch (e.key.keysym.sym) {
- case SDLK_UP:
- current = image[KEY_SURFACE_UP];
- SDL_BlitSurface(current, NULL, surface, NULL);
- SDL_UpdateWindowSurface(window);
- break;
- case SDLK_DOWN:
- current = image[KEY_SURFACE_DOWN];
- SDL_BlitSurface(current, NULL, surface, NULL);
- SDL_UpdateWindowSurface(window);
- break;
- case SDLK_LEFT:
- current = image[KEY_SURFACE_LEFT];
- SDL_BlitSurface(current, NULL, surface, NULL);
- SDL_UpdateWindowSurface(window);
- break;
- case SDLK_RIGHT:
- current = image[KEY_SURFACE_RIGHT];
- SDL_BlitSurface(current, NULL, surface, NULL);
- SDL_UpdateWindowSurface(window);
- break;
- default:
- current = image[KEY_SURFACE_DEFAULT];
- SDL_BlitSurface(current, NULL, surface, NULL);
- SDL_UpdateWindowSurface(window);
- break;
- }
- }
- }
- }
- }
- }
- // close(&window, image);
- delete [] image;
- return 0;
- }
- bool init(SDL_Window **window, SDL_Surface **surface) {
- bool state = true;
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- state = false;
- else {
- *window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (*window == NULL)
- state = false;
- else
- *surface = SDL_GetWindowSurface(*window);
- }
- return state;
- }
- SDL_Surface* loadSurface(std::string name) {
- SDL_Surface* surface;
- if ((surface = SDL_LoadBMP(name.c_str())) == NULL)
- std::cout << "Error while loading surface: " << SDL_GetError() << std::endl;
- else
- return surface;
- }
- bool loadMedia(SDL_Surface **image){//[KEY_SURFACE_TOTAL]) {
- bool state = true;
- SDL_Surface **temp = image;
- *image = loadSurface("default.bmp");
- if (image++ == NULL)
- state = false;
- *image = loadSurface("up.bmp");
- if (image++ == NULL)
- state = false;
- *image = loadSurface("down.bmp");
- if (image++ == NULL)
- state = false;
- *image = loadSurface("left.bmp");
- if (image++ == NULL)
- state = false;
- *image = loadSurface("right.bmp");
- if (image++ == NULL)
- state = false;
- image = temp;
- return state;
- }
- bool close(SDL_Window **window, SDL_Surface **surface) {
- SDL_FreeSurface(*surface); *surface = NULL;
- SDL_DestroyWindow(*window); *window = NULL;
- SDL_Quit();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement