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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#include <SDL.h>
#include <SDL_image.h>
#include <string>
#include <SDL_ttf.h>
SDL_Surface* screen=NULL;
SDL_Surface* background=NULL;
SDL_Surface* message=NULL;
SDL_Surface* buttonSheet=NULL;
TTF_Font* font = NULL;
SDL_Color fontcolor = {0,0,0};
bool quit = false;
SDL_Rect clop[2];
SDL_Event event;
void setclops()
{
clop[0].x=0;
clop[0].y=0;
clop[0].w=37;
clop[0].h=17;
clop[1].x=37;
clop[1].y=0;
clop[1].w=74;
clop[1].h=17;
}
void applysurface(int x, int y, SDL_Surface* src, SDL_Surface* dst, SDL_Rect* clip = NULL)
{
SDL_Rect offset;
offset.x=x;
offset.y=y;
SDL_BlitSurface(src,clip,dst,&offset);
}
SDL_Surface* loadimg(std::string filename)
{
SDL_Surface* img1;
img1 =IMG_Load(filename.c_str());
if(img1 != NULL)
{
img1 = SDL_DisplayFormat(img1);
SDL_SetColorKey(img1,SDL_SRCCOLORKEY,SDL_MapRGB(img1->format, 0xFF,0xFF,0xFF));
}
return img1;
}
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING)==-1 || TTF_Init() == -1)
return false;
SDL_WM_SetCaption("ahem",NULL);
screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE);
background = loadimg("background.png");
buttonSheet = loadimg("butt.png");
font = TTF_OpenFont("lazy.ttf",20);
message = TTF_RenderText_Solid(font,"Press 'X' to exit",fontcolor);
void setclops();
if(screen == NULL || background == NULL || message == NULL || font == NULL)
return false;
return true;
}
void bye()
{
SDL_FreeSurface(message);
SDL_FreeSurface(background);
SDL_FreeSurface(buttonSheet);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}
int main(int argc, char* args[])
{
if(init() == false)
return 1;
applysurface(0,0,background,screen);
applysurface(150,150,buttonSheet,screen,&clop[0]);
applysurface(202,229,message,screen);
if(SDL_Flip(screen)==-1)
return 1;
while(quit ==false)
{
if(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
message = TTF_RenderText_Solid(font,"nothing happend",fontcolor);
applysurface(202,329,message,screen);
}
if(event.type == SDL_MOUSEMOTION)
{
int x= event.motion.x;
int y= event.motion.y;
if(x>591 && x<628 && y>12 && y<29)
applysurface(591,120,buttonSheet,screen,&clop[1]);
else
applysurface(591,12,buttonSheet,screen,&clop[0]);
if(SDL_Flip(screen) ==-1)
return 1;
}
if(event.type == SDL_MOUSEBUTTONDOWN)
{
int x= event.motion.x;
int y= event.motion.y;
if(x>591 && x<628 && y>12 && y<29)
applysurface(591,12,buttonSheet,screen,&clop[0]);
if(SDL_Flip(screen) ==-1)
return 1;
}
if(event.type == SDL_MOUSEBUTTONUP)
{
int x= event.motion.x;
int y= event.motion.y;
if(x>591 && x<628 && y>12 && y<28)
quit = true;
}
}
}
bye();
return 0;
}
|