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
|
#include "stdafx.h"
//screen dimensions
const int screen_width = 800;
const int screen_height = 600;
const int screen_bpp = 32;
//button location
SDL_Rect buttonLocation;
//surfaces
SDL_Surface* background = NULL;
SDL_Surface* paddle = NULL;
SDL_Surface* button = NULL;
SDL_Surface* button2 = NULL;
SDL_Surface* ball = NULL;
SDL_Surface* message = NULL;
SDL_Event event;
TTF_Font* font = NULL;
SDL_Color textColor = {255,255,255};
int main(int argc, char *args[])
{
//quit for the event loop and running for the timer
bool quit = false;
bool running = false;
bool mouseMoving = false;
//ints to hold the x and y cords of mouse
int xCord= 0, yCord= 0;
buttonLocation.x = 200;
buttonLocation.y = 200;
// initialize sdl and ttf
if (sdl_initWithScreenDimensions(screen_width, screen_height, screen_bpp) == false)
{
return 1;
}
//load the font
font = TTF_OpenFont("newfont.ttf", 22);
//load the surfaces
background = load_image("background.png");
button = load_image("button.png");
button2 = load_image("button2.png");
//apply the initial surfaces
apply_surface(0, 0, background, screen);
apply_surface(buttonLocation.x, buttonLocation.y, button, screen);
//get the Timer
Timer myTimer;
//start the clock
myTimer.start();
while(quit == false)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
if (event.type == SDL_MOUSEBUTTONDOWN)
{
mouseMoving = true;
}
if (event.type == SDL_MOUSEMOTION)
{
int x, y;
mouseMoving == true;
x = event.motion.x;
y = event.motion.y;
//if x cord of mouse is within x cords of button and y cords
if ((x >= 200 && (x <= 200+(button->w)))&& (y >=200 && (y <= 200+(button->h))))
{
//do this
apply_surface(0, 0, background, screen);
apply_surface((screen_width - button->w)/2, (screen_height - button->h)/2, button2, screen);
}
else
{
//do this
apply_surface(0, 0, background, screen);
apply_surface((screen_width - button->w)/2, (screen_height - button->h)/2, button, screen);
}
}
}
if (mouseMoving== true)
{
std::stringstream mouseCords;
xCord = event.motion.x;
yCord = event.motion.y;
mouseCords<< "Mouse Location: ("<< xCord << ","<< yCord << ")";
message = TTF_RenderText_Solid(font, mouseCords.str().c_str(), textColor);
apply_surface(10, 10, message, screen);
SDL_FreeSurface(message);
}
if (SDL_Flip(screen) == -1)
{
return 1;
}
}
return 0;
}
|