Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SDL_HelloWorld.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <SDL.h>
- bool init();
- bool loadMedia();
- void close();
- //The window to render to
- SDL_Window* gWindow = nullptr;
- //The surface contained by the window
- SDL_Surface* gScreenSurface = nullptr;
- //The image to load and show on screen
- SDL_Surface* gHelloWorld = nullptr;
- int main(int argc, char *argv[])
- {
- if (!init())
- {
- std::cerr << "Failed to initialize.\n";
- }
- else
- {
- //Load media
- if (!loadMedia())
- {
- std::cerr << "Failed to load media.\n";
- }
- else
- {
- SDL_BlitSurface(
- gHelloWorld,
- NULL,
- gScreenSurface,
- NULL
- );
- // Update the surface
- SDL_UpdateWindowSurface( gWindow );
- //Wait two seconds
- SDL_Delay( 2000 );
- }
- }
- //Free resources and close SDL
- close();
- return 0;
- }
- bool init()
- {
- bool success = true;
- // Initialize SDL
- if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
- {
- std::cerr << "Could not initialize SDL. SDL Error: " << SDL_GetError() << "\n";
- success = false;
- }
- else
- {
- gWindow = SDL_CreateWindow(
- "SDL Tutorial",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- 640,
- 480,
- SDL_WINDOW_SHOWN
- );
- if (gWindow == NULL)
- {
- std::cerr << "Window could not be created. SDL Error: " << SDL_GetError() << "\n";
- success = false;
- }
- else
- {
- //Get window surface
- gScreenSurface = SDL_GetWindowSurface(gWindow);
- }
- }
- return success;
- }
- bool loadMedia()
- {
- //Load success flag
- bool success = true;
- //Load splash image
- gHelloWorld = SDL_LoadBMP( "./res/imageToLoad.bmp" );
- if (gHelloWorld == NULL)
- {
- std::cerr << "unable to load image ./res/imageToLoad.bmp: " << SDL_GetError() << "\n";
- success = false;
- }
- return success;
- }
- void close()
- {
- //deallocate surface
- SDL_FreeSurface( gHelloWorld );
- gHelloWorld = nullptr;
- //Destroy Window
- SDL_DestroyWindow( gWindow );
- gWindow = nullptr;
- //Quit SDL
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement