Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <stdio.h>
- #include <string>
- const int SCREEN_HEIGHT = 768;
- const int SCREEN_WIDTH = 1024;
- bool init();
- bool loadMedia();
- void close();
- SDL_Surface* loadSurface(std::string path);
- SDL_Surface* gScreenSurface = NULL;
- SDL_Surface* gStretchedSurface = NULL;
- SDL_Window* gWindow = NULL;
- int main(int argc, char* args[])
- {
- if (!init())
- {
- printf("SDL couldn't initialize");
- }
- else
- {
- if (!loadMedia())
- {
- printf("couldn't load media");
- }
- else
- {
- bool quit = false;
- SDL_Event e;
- while (!quit)
- {
- while (SDL_PollEvent(&e) != 0)
- {
- if (e.type == SDL_QUIT)
- {
- quit = true;
- }
- }
- //apply stretched image
- SDL_Rect stretchRect;
- stretchRect.x = 0;
- stretchRect.y = 0;
- stretchRect.w = SCREEN_WIDTH;
- stretchRect.h = SCREEN_HEIGHT;
- SDL_BlitScaled(gStretchedSurface, NULL, gScreenSurface, &stretchRect);
- SDL_UpdateWindowSurface(gWindow);
- }
- }
- }
- close();
- return 0;
- }
- bool init()
- {
- bool success = true;
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- printf("SDL Couldn't initialize.");
- success = false;
- }
- else
- {
- gWindow = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (gWindow == NULL)
- {
- printf("SDL Window failure.");
- success = false;
- }
- else
- {
- gScreenSurface = SDL_GetWindowSurface(gWindow);
- //SDL_Delay(100000);
- }
- }
- return success;
- }
- bool loadMedia()
- {
- bool success = true;
- gStretchedSurface = loadSurface("stretch.bmp");
- if (gStretchedSurface == NULL)
- {
- printf("Couldnt load screen surface");
- success = false;
- }
- return success;
- }
- SDL_Surface* loadSurface(std::string path)
- {
- //final optimized Image
- SDL_Surface* optimizedSurface = NULL;
- //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());
- }
- else
- {
- //convert surface to screen format
- optimizedSurface = SDL_ConvertSurface(loadedSurface, gScreenSurface->format, NULL);
- if (optimizedSurface == NULL)
- {
- printf("Unable to optimize image %s! SDL error: ", path.c_str(), SDL_GetError());
- }
- //get rid of old loaded surface
- SDL_FreeSurface(loadedSurface);
- }
- return optimizedSurface;
- }
- void close()
- {
- SDL_FreeSurface(gStretchedSurface);
- gStretchedSurface = nullptr;
- SDL_DestroyWindow(gWindow);
- gWindow = nullptr;
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement