1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include <fstream>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if (loadedImage != NULL)
{
optimizedImage = loadedImage;
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, NULL, destination, &offset );
}
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return false;
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if (screen == NULL)
return false;
SDL_WM_SetCaption("My First SDL App", NULL);
return true;
}
bool load_files()
{
image = load_image("first.png");
if (image == NULL)
return false;
return true;
}
void clean_up()
{
SDL_FreeSurface(image);
SDL_Quit();
}
int main(int argc, char* args[])
{
std::fstream statuslog;
statuslog.open ("statuslog.txt", std::fstream::out);
statuslog << "Opening log...\n";
statuslog.close();
bool quit = false;
statuslog.open ("statuslog.txt", std::fstream::out);
statuslog << "Beginning statuslog...\n";
statuslog.close();
if (init() == false)
return 1;
statuslog.open ("statuslog.txt", std::fstream::out);
statuslog << "System has been intialized\n";
statuslog.close();
if (load_files() == false)
return 1;
statuslog.open ("statuslog.txt", std::fstream::out);
statuslog << "Files have been loaded\n";
statuslog.close();
apply_surface(0, 0, image, screen);
statuslog.open ("statuslog.txt", std::fstream::out);
statuslog << "Surface has been applied\n";
statuslog.close();
if (SDL_Flip(screen) == -1)
return 1;
statuslog.open ("statuslog.txt", std::fstream::out);
statuslog << "Screen has been flipped\n";
statuslog.close();
while (quit == false)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
quit = true;
}
}
statuslog.open ("statuslog.txt", std::fstream::out);
statuslog << "The process has been completed successfully\n";
statuslog.close();
clean_up();
return 0;
}
|