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
|
#include <string>
#include <iostream>
#include <SDL.h>
using namespace std;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *message = NULL;
SDL_Surface *bkg = NULL;
SDL_Surface *screen = NULL;
class box
{
private:
public:
box(int x, int y, int xx, int yy);
int x1;
int y1;
int x2;
int y2;
};
box::box(int x, int y, int xx, int yy)
{
x1 = x;
x2 = xx;
y1 = y;
y2 = yy;
}
SDL_Surface* loadImage(string fileName)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(fileName.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void applySurface(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);
}
int main(int argc, char ** argv)
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0);
{
cout << "SDL_Init Failed.\n";
cout << "Error: " << SDL_GetError() << endl;
// return 1;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if(screen == NULL)
{
cout << "SDL_SetVideoMode failed.\n";
return 1;
}
SDL_WM_SetCaption("HelloW.bkgImage", NULL);
message = loadImage("hello.bmp");
bkg = loadImage("arrowLeft.bmp");
box fxf(0,0,320,240);
applySurface(fxf.x1, fxf.y1, bkg, screen);
applySurface(fxf.x2, fxf.y1, bkg, screen);
applySurface(fxf.x1, fxf.y2, bkg, screen);
applySurface(fxf.x2, fxf.y2, bkg, screen);
applySurface(fxf.x2 / 2, fxf.y2 / 2, message, screen);
bool run = true;
while(run)
{
fxf.x1 ++;
fxf.x2 ++;
fxf.y1 ++;
// fxf.y2 ++;
// SDL_Delay(50);
if(SDL_Flip(screen) == -1)
{
cout << "SDL_FLIP (draw) failed.\n";
return 1;
}
applySurface(fxf.x1, fxf.y1, bkg, screen);
applySurface(fxf.x2, fxf.y1, bkg, screen);
applySurface(fxf.x1, fxf.y2, bkg, screen);
applySurface(fxf.x2, fxf.y2, bkg, screen);
applySurface(fxf.x2 / 2, fxf.y2 / 2, message, screen);
if(fxf.x1 >= SCREEN_WIDTH)
{
run = false;
}
}
SDL_FreeSurface(message);
SDL_FreeSurface(bkg);
SDL_Quit();
return 0;
}
|