Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // emcc source.cpp -s USE_SDL=2 -s FULL_ES2=1 --preload-file res/img -o publish\emsripten\index.html -O2 -s ALLOW_MEMORY_GROWTH=1
- #include <SDL.h>
- #include <stdio.h>
- #include <string>
- #ifdef __EMSCRIPTEN__
- #include <emscripten.h>
- #endif
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 480;
- enum KeyPressSurfaces
- {
- KEY_PRESS_SURFACE_DEFAULT,
- KEY_PRESS_SURFACE_UP,
- KEY_PRESS_SURFACE_DOWN,
- KEY_PRESS_SURFACE_LEFT,
- KEY_PRESS_SURFACE_RIGHT,
- KEY_PRESS_SURFACE_TOTAL
- };
- bool init();
- bool loadMedia();
- void close();
- SDL_Surface* loadSurface(std::string path);
- SDL_Window* gWindow = NULL;
- SDL_Surface* gScreenSurface = NULL;
- //SDL_Surface* gXOut = NULL;
- SDL_Surface* gKeyPressSurfaces[KEY_PRESS_SURFACE_TOTAL];
- SDL_Surface* gCurrentSurface = NULL;
- SDL_Event e;
- bool quit = false;
- SDL_Surface* loadSurface(std::string path)
- {
- //Load image at specified path
- SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
- if (loadedSurface == NULL)
- {
- printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
- }
- return loadedSurface;
- }
- bool init()
- {
- //Initialization flag
- bool success = true;
- //Initialize SDL
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- //Create window
- gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (gWindow == NULL)
- {
- printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- //Get window surface
- gScreenSurface = SDL_GetWindowSurface(gWindow);
- }
- }
- return success;
- }
- bool loadMedia()
- {
- //Loading success flag
- bool success = true;
- //Load default surface
- gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("./res/img/press.bmp");
- if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == NULL)
- {
- printf("Failed to load default image!\n");
- success = false;
- }
- //Load up surface
- gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = loadSurface("./res/img/up.bmp");
- if (gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == NULL)
- {
- printf("Failed to load up image!\n");
- success = false;
- }
- //Load down surface
- gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = loadSurface("./res/img/down.bmp");
- if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == NULL)
- {
- printf("Failed to load down image!\n");
- success = false;
- }
- //Load left surface
- gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] = loadSurface("./res/img/left.bmp");
- if (gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] == NULL)
- {
- printf("Failed to load left image!\n");
- success = false;
- }
- //Load right surface
- gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] = loadSurface("./res/img/right.bmp");
- if (gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] == NULL)
- {
- printf("Failed to load right image!\n");
- success = false;
- }
- gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
- return success;
- }
- void close()
- {
- //Deallocate surfaces
- for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i)
- {
- SDL_FreeSurface(gKeyPressSurfaces[i]);
- gKeyPressSurfaces[i] = NULL;
- }
- //Destroy window
- SDL_DestroyWindow(gWindow);
- gWindow = NULL;
- //Quit SDL subsystems
- SDL_Quit();
- }
- void main_loop()
- {
- //Handle events on queue
- while (SDL_PollEvent(&e) != 0)
- {
- //User requests quit
- if (e.type == SDL_QUIT)
- {
- quit = true;
- }
- //User presses a key
- else if (e.type == SDL_KEYDOWN)
- {
- //Select surfaces based on key press
- switch (e.key.keysym.sym)
- {
- case SDLK_UP:
- gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
- break;
- case SDLK_DOWN:
- gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
- break;
- case SDLK_LEFT:
- gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
- break;
- case SDLK_RIGHT:
- gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
- break;
- default:
- gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
- break;
- }
- }
- }
- //Apply the image
- SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, NULL);
- //Update the surface
- SDL_UpdateWindowSurface(gWindow);
- }
- int main(int argc, char* args[])
- {
- //Start up SDL and create window
- if (!init())
- {
- printf("Failed to initialize!\n");
- }
- else
- {
- //Load media
- if (!loadMedia())
- {
- printf("Failed to load media!\n");
- }
- else
- {
- #ifdef __EMSCRIPTEN__
- emscripten_set_main_loop(main_loop, 0, 1);
- #else
- //While application is running
- while (!quit)
- {
- main_loop();
- }
- SDL_Delay(100);
- #endif
- }
- }
- //Free resources and close SDL
- close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement