Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <iostream>
- using namespace std;
- const int SCREEN_WIDTH = 800;
- const int SCREEN_HEIGHT = 600;
- enum KeyPressSurfaces
- {
- Up,
- Down,
- Left,
- Right,
- Default,
- Total
- };
- SDL_Window *gWindow = nullptr;
- SDL_Surface *gWindowSurface = nullptr;
- SDL_Surface* gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Total)];
- SDL_Surface *gCurrentSurface = nullptr;
- bool init();
- SDL_Surface *loadSurface(string path);
- bool loadMedia();
- bool render();
- void keyboardEvents(SDL_Event &e);
- void close();
- int main(int, char **)
- {
- bool success = true;
- success = init();
- if (true == success)
- {
- success = loadMedia();
- }
- if (true == success)
- {
- while (true == render());
- }
- close();
- return 0;
- }
- bool init()
- {
- bool success = true;
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- cout << "SDL init failed. " << SDL_GetError() << endl;
- success = false;
- }
- if (true == success) {
- gWindow = SDL_CreateWindow(u8"Klawiszki kurwa", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (gWindow == nullptr)
- {
- cout << "Window creation failed. " << SDL_GetError() << endl;
- success = false;
- }
- }
- if (true == success) {
- gWindowSurface = SDL_GetWindowSurface(gWindow);
- }
- return success;
- }
- SDL_Surface *loadSurface(string path)
- {
- SDL_Surface *loadedSurface = SDL_LoadBMP(path.c_str());
- if (nullptr == loadedSurface) {
- cout << "Could not load file: " << path.c_str() << " SDL error: " << SDL_GetError() << endl;
- }
- return loadedSurface;
- }
- bool loadMedia()
- {
- bool success = true;
- string pliki[]{ "up.bmp","down.bmp","left.bmp","right.bmp","default.bmp" };
- for (int i = 0; i < 5; i++) {
- success = gKeyPressSurfaces[i] = loadSurface(pliki[i]);
- if (false == success) {
- break;
- }
- }
- if (true == success) {
- gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Default)];
- }
- return success;
- }
- bool render()
- {
- bool quit = false;
- SDL_Event e;
- while (0 != SDL_PollEvent(&e))
- {
- if (SDL_QUIT == e.type)
- {
- quit = true;
- }
- else {
- keyboardEvents(e);
- }
- }
- SDL_Rect rect { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
- SDL_BlitScaled(gCurrentSurface, nullptr, gWindowSurface, &rect);
- SDL_UpdateWindowSurface(gWindow);
- return true == quit ? false : true;
- }
- void keyboardEvents(SDL_Event &e)
- {
- switch (e.key.keysym.sym)
- {
- case SDLK_UP:
- gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Up)];
- break;
- case SDLK_DOWN:
- gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Down)];
- break;
- case SDLK_LEFT:
- gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Left)];
- break;
- case SDLK_RIGHT:
- gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Right)];
- break;
- default:
- gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Default)];
- break;
- }
- }
- void close()
- {
- gCurrentSurface = nullptr;
- for (int i = 0; i < 5; i++) {
- SDL_FreeSurface(gKeyPressSurfaces[i]);
- gKeyPressSurfaces[i] = nullptr;
- }
- SDL_DestroyWindow(gWindow);
- gWindow = nullptr;
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement