Code: Select all
#include"stdafx.h"
#include"SDL.h"
#include"SDL_ttf.h"
#include"SDL_image.h"
#include<vector>
#include <string>
#include <sstream>
using namespace std;
// It just doesn't move the box when I press the keys!
const int SQUARE_WIDTH = 20;
const int SQUARE_HEIGHT = 20;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 30;
SDL_Surface *background = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *collideMessage = NULL;
SDL_Surface *missMessage = NULL;
SDL_Surface *seconds = NULL;
SDL_Surface *boxgraphic = NULL;
//The event structure
SDL_Event event;
//The font
TTF_Font *font = NULL;
//The color of the font
SDL_Color textColor = {0, 0, 0 };
SDL_Rect wall;
bool quit = false;
class Timer {
private:
int startTicks, pausedTicks;
bool paused, started;
public:
Timer();
void start();
void stop();
void pause();
void unpause();
int get_ticks();
bool is_started();
bool is_paused();
};
Timer::Timer() {
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start() {
started = true;
paused = false;
startTicks = SDL_GetTicks();
};
void Timer::stop() {
started = false;
paused = false;
}
int Timer::get_ticks() {
if (started == true ) {
if (paused == true ) {
return pausedTicks;
}
else { return SDL_GetTicks() - startTicks;
}
}
return 0;
}
void Timer::pause() {
if (started == true && paused == false) {
paused = true;
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause() {
if (paused == true) {
paused = false;
startTicks = SDL_GetTicks() - pausedTicks;
pausedTicks = 0;
}
}
bool Timer::is_started() {
return started;
}
bool Timer::is_paused() {
return paused;
}
class Square {
private:
SDL_Rect box;
int xVel,yVel;
public:
Square();
void move();
void show();
void handle_input();
};
Square::Square () {
box.x = 25;
box.y = 25;
box.w = SQUARE_WIDTH;
box.h = SQUARE_HEIGHT;
xVel, yVel = 0;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect * clip = NULL) {
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool check_collision(SDL_Rect A, SDL_Rect B) { //The sides of the rectangles
int leftA,leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Go through the A boboxes
//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
//Go through the B boboxes
//Calculate the sides of rect B
leftB = B.y;
rightB = B.y + B.w;
topB = B.y;
bottomB = B.y + B.h;
//If no sides from A are outside of B
if( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ){
//A collision is detected
return true;
}
else {
//If neither set of collision boboxes touched
return false;
}
}
void Square::move () {
box.x += xVel;
if ( (box.x <= 0) || (box.x + SQUARE_WIDTH > SCREEN_WIDTH) || check_collision( box, wall) ) {
box.x -= xVel;
}
box.y += yVel;
if ( (box.y <= 0) || (box.y + SQUARE_HEIGHT > SCREEN_HEIGHT) || check_collision( box, wall) ) {
box.y -= yVel;
}
}
void Square::handle_input() {
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;
}
}
//If a key was released
if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;
}
}
}
void Square::show()
{
//Show the square
apply_surface( box.x, box.y, boxgraphic, screen);
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( missMessage );
SDL_FreeSurface( collideMessage );
SDL_FreeSurface (boxgraphic);
SDL_FreeSurface (seconds);
SDL_FreeSurface (screen);
//Close the font
TTF_CloseFont( font );
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = SDL_LoadBMP( filename.c_str() );
//If the image loaded
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 the optimized surface
return optimizedImage;
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Press an Arrow Key", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the background image
background = load_image( "background.bmp" );
boxgraphic = load_image( "Square.bmp" );
font = TTF_OpenFont( "lazy.ttf", 20 );
//Open the font
//If there was a problem in loading the background
if( background == NULL || boxgraphic == NULL )
{
return false;
}
//If there was an error in loading the font
if( font == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
int main ( int argc, char* args[] ) {
int frame = 0;
Square mySquare;
bool running = false;
Timer fps;
Timer update;
if (init() == false) {
return false;
}
if (load_files() == false ) {
return false;
}
wall.x = 300;
wall.y = 40;
wall.w = 40;
wall.h = 400;
//Generate the message surfaces
collideMessage = TTF_RenderText_Solid( font, "Collision detected!", textColor );
missMessage = TTF_RenderText_Solid( font, "No collisions detected.", textColor );
message = TTF_RenderText_Solid( font, "Testing Frame Rate", textColor );
//While the user hasn't quit
while( quit == false )
{
update.start();
fps.start();
//If message needs to be displayed
std::stringstream time;
//If there's an event to handle
if( SDL_PollEvent( &event ) )
{
mySquare.handle_input();
if( event.type == SDL_QUIT )
{
fps.stop();
update.stop();
//Quit the program
quit = true;
}
}
mySquare.move();
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF,0xFF, 0xFF ) );
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 0x77, 0xFF, 0x77 ) );
mySquare.show();
if (running == true ) {
time << "Timer: " << fps.get_ticks();
}
seconds = TTF_RenderText_Solid( font, time.str().c_str(), textColor);
apply_surface ( 300,20,seconds,screen);
apply_surface( ( SCREEN_WIDTH - message->w ) / 2, ( ( SCREEN_HEIGHT + message->h * 2 ) / FRAMES_PER_SECOND ) * ( frame % FRAMES_PER_SECOND ) - message->h, message, screen );
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
frame+=1;
if ( update.get_ticks() > 1000 ) {
std::stringstream caption;
caption << "Average number of frames per second " << frame / ( fps.get_ticks() / 1000.f );
SDL_WM_SetCaption ( caption.str().c_str(), NULL );
update.start();
}
}
//Clean up
clean_up();
return 0;
}
I also don't know how to debug an SDL program using a breakpoint, when I set one during a loop. It stops, minimizes the SDL program to the taskbar,then I can't open up the program to send a keypress! How would you do something like that?
The other thing I wanted to talk about was I have a lot of what I consider to be good ideas for games, and yet they keep getting turned into something in another game by a big developer, before I've even finished college, or made a game. Its really frustrating and disappointing. What would you guys say about that?
And lastly, I would love to have some friends who are into game development, and maybe start a project, except I don't have any good friends who are into all that! I'm above searching online for team members, because for one thing the majority of "applicants" are kids who know nothing and think it would be great to make a game but lack the talent. Any legitimate team members would be online, at risk to leak things, in my experience,and located halfway around the world! I may be just a beginner, but as you can tell I've got some big plans. I am quite an introvert, but I'm crawling out of my cave... I would like to make friends at school who are into this, I don't know how to find them. No computer classes yet...