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
|
#include <iostream>
#include <string>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <sstream>
using namespace std;
bool quit = false;
engine eng;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 60;
SDL_Surface* screen = NULL;
SDL_Surface* Mousecoords;
TTF_Font* font = NULL;
SDL_Color textColor = {255,255,255};
SDL_Event event;
SDL_Rect camera = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT};
SDL_Surface *load_image(std::string filename)
{
SDL_Surface* loadedimage = NULL;
SDL_Surface* optimizedimage = NULL;
loadedimage = IMG_Load(filename.c_str());
if( loadedimage != NULL )
{
//Create an optimized surface
optimizedimage = SDL_DisplayFormat( loadedimage );
//Free the old surface
SDL_FreeSurface( loadedimage );
//If the surface was optimized
if( optimizedimage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedimage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedimage->format, 0, 0xFF, 0xFF ) );
}
}
return optimizedimage;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, clip, 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;
}
if(TTF_Init()==-1)
{
return false;
}
SDL_WM_SetCaption("Where's my mouse?", NULL);
return true;
}
bool load_files()
{
font = TTF_OpenFont("Ubuntu-L.ttf",28);
if(font == NULL)
{
return false;
}
return true;
}
void clean_up()
{
SDL_FreeSurface(screen);
SDL_FreeSurface(Mousecoords);
TTF_CloseFont(font);
TTF_Quit;
SDL_Quit;
}
int main(int argc, char* args[])
{
if(init()==false)
{
return 1;
}
if(load_files()==false)
{
return 2;
}
std::stringstream xcoord;
std::stringstream ycoord;
while(quit == false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_MOUSEMOTION)
{
SDL_FreeSurface(Mousecoords);
int x = 0;
int y = 0;
x = event.motion.x;
y = event.motion.y;
xcoord << x;
ycoord << y;
Mousecoords = TTF_RenderText_Solid(font, xcoord.str().c_str(), textColor);
apply_surface(0,0,Mousecoords,screen);
if(SDL_Flip(screen) == -1)
{
return 0;
}
}
if(event.type == SDL_QUIT)
{
quit = true;
}
}
if(SDL_Flip(screen) == -1)
{
return 0;
}
}
clean_up();
return 0;
}
|