Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SDL.h>
- #include <stdio.h>
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 480;
- // returns false if initializing SDL fails or if creating windows fails
- bool init(SDL_Window* p_gWindow, SDL_Surface* p_gScreenSurface)
- {
- //Initialization flag
- bool success = true;
- //Initialize SDL
- //checks if SDL_Init returns -1
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- //Create window
- p_gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
- SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- //checks if SDL_CreateWindow returned null meaning that they couldn't make a window
- if (p_gWindow == NULL)
- {
- printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- //Get window surface
- p_gScreenSurface = SDL_GetWindowSurface(p_gWindow);
- }
- }
- return success;
- }
- // return false if SDL_LoadBMP FAILED. Else it will return true
- bool loadMedia(SDL_Surface* gImage)
- {
- //Loading success flag
- bool success = true;
- //Load splash image
- gImage = SDL_LoadBMP("background.bmp");
- if (gImage == NULL)
- {
- printf("Unable to load image! SDL Error: %s\n", SDL_GetError());
- success = false;
- }
- return success;
- }
- void close(SDL_Surface* p_gimage, SDL_Window* p_gWindow)
- {
- //Deallocate surface
- SDL_FreeSurface(p_gimage);
- p_gimage = NULL;
- //Destroy window
- SDL_DestroyWindow(p_gWindow);
- p_gWindow = NULL;
- //Quit SDL subsystems
- SDL_Quit();
- }
- //main
- int main(int argc, char* args[])
- {
- //The window we'll be rendering to
- SDL_Window* gWindow = NULL;
- //The surface contained by the window
- SDL_Surface* gScreenSurface = NULL;
- //The image we will load and show on the screen
- SDL_Surface* gImage = NULL;
- //Start up SDL and create window
- if (!init(gWindow, gScreenSurface))
- {
- printf("Failed to initialize!\n");
- }
- else
- {
- //Load media
- if (!loadMedia(gImage))
- {
- printf("Failed to load media!\n");
- }
- else
- {
- //Main loop flag
- bool quit = false;
- //Event handler
- SDL_Event e;
- while (!quit) {
- while (SDL_PollEvent(&e) != 0)
- {
- if (e.type == SDL_QUIT)
- {
- quit = true;
- }
- }
- //Apply the image
- SDL_BlitSurface(gImage, NULL, gScreenSurface, NULL);
- //Update the surface
- SDL_UpdateWindowSurface(gWindow);
- }
- }
- }
- //Free resources and close SDL
- close(gImage, gWindow);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement