#2 Members - Reputation: 372
Posted 29 December 2012 - 08:51 AM
Im trying to program a game with Visual C++ and SDL but without success. Do you know where find ebook about those themes?
Many publishers such as O'Reilly sell books in ebook format. Could you be more specific as to what you need focus on? Are you searching for a book covering the Visual C++ IDE? SDL? Programming in general?
#3 Crossbones+ - Reputation: 1373
Posted 29 December 2012 - 12:44 PM
Im trying to program a game with Visual C++ and SDL but without success. Do you know where find eBook about those themes?
Many publishers such as O'Reilly sell books in eBook format. Could you be more specific as to what you need focus on? Are you searching for a book covering the Visual C++ IDE? SDL? Programming in general?
I am a member of the O'Reilly Blogger Review program, so I get free books. I have to review every book I write in my Blog, however it's worth it. Considering I otherwise can't afford many books, it's a great system. You should look into that program if you're short on cash.
Here's Breakout:
Breakout!
If you need some photo editing done, contact me:
superman3275@gmail.com
if you want some programming help, or are recruiting for a game development team, either PM me on here or email me up there
#4 Members - Reputation: 122
Posted 29 December 2012 - 01:31 PM
dmreichard: You´re right, I didn´t specify about my problem. I am following the tutorials in lazyfoo.net/SDL_tutorials/index.php
I was writtting the lesson 16, downloaded the source code (at the bottom of the page) to rewrite it and separate the interface from the implementation. After rewrite all the source code the class Dot didnt have access to the global variables declarated in the file with the main.cpp.
Im pretty shure this its a really dumb problem but since Im noob with de SDL library it´s very hard to me.
superman3275: Didnt know about that.. its a sweet deal. I will try to do that, thanks ![]()
//main.cpp
#include "SDL_image.h"
#include "timer.h"
#include "dot.h"
#include <string>
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 20;
//The dimensions of the dot
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;
//The surfaces
SDL_Surface *dot = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
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 = IMG_Load( 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, 0xFF, 0, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
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 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;
}
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the image
dot = load_image("dot.bmp");
//If there was a problem in loading the dot
if(dot == NULL)
return false;
//If everything loaded fine
return true;
}
void clean_up()
{
SDL_FreeSurface( dot );
//Quit SDL
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//The dot that will be used
Dot myDot;
//The frame rate regulator
Timer fps;
//Initializate
if( init() == false )
return 1;
//Load the files
if( load_files() == false )
return 1;
//While the user hasn´t quit
while( quit == false )
{
//If there´s events to handle
if( SDL_PollEvent ( &event ) )
{
//Handle events for the dot
myDot.handle_input();
//If Xed was press
if(event.type == SDL_QUIT)
{
//Close the window
quit = true;
}
}//PollEvent
//Move the dot
myDot.move();
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF) );
//Show the dot on the screen
myDot.show();
//Update the screen
if( SDL_Flip( screen ) == -1)
return 1;
//Cap the frame rate
if(fps.get_ticks() < 1000/FRAMES_PER_SECOND)
{
SDL_Delay( (1000/FRAMES_PER_SECOND)-fps.get_ticks() );
}
}//while
//Clean up
clean_up();
return 0;
}
//dot.h
#ifndef DOT_H
#define DOT_H
class Dot
{
public:
Dot();
void handle_input();
void move();
void show();
private:
int x;
int y;
int xVel;
int yVel;
}
#endif
//dot.cpp
#include "dot.h"
#include "SDL.h"
#include "SDL_image.h"
Dot::Dot()
{
x = 0;
y = 0;
xVel = 0;
yVel = 0;
}
void Dot::handle_input()
{
if(event.type == SDL_KEYDOWN)
{
//Adjust the velocity
switch(event.key.keysym.sym)
{
case SDLK_UP: yVel -= DOT_HEIGHT /2; break;
case SDLK_DOWN: yVel += DOT_HEIGHT /2; break;
case SDLK_LEFT: xVel -= DOT_HEIGHT /2; break;
case SDLK_RIGHT: xVel += DOT_HEIGHT /2; break;
}
}
//If a key was released
else if(event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_UP: yVel += DOT_HEIGHT /2; break;
case SDLK_DOWN: yVel -= DOT_HEIGHT /2; break;
case SDLK_LEFT: xVel += DOT_HEIGHT /2; break;
case SDLK_RIGHT: xVel -= DOT_HEIGHT /2; break;
}
}
}
void Dot::move()
{
//Move the dot right or left
x += xVel;
//If the dot went too far to the left or right
if( (x < 0) || (x + DOT_WIDTH > SCREEN_WIDTH) )
{
//move back
x -= xVel;
}
//Move the dot up or down
y += yVel;
//If the dot went too far to the left or right
if( (y < 0) || (x + DOT_HEIGHT > SCREEN_HEIGHT) )
{
//move back
y -= yVel;
}
}
void Dot::show()
{
apply_surface(x, y, dot, screen);
}
//timer.h
#ifndef TIMER_H
#define TIMER_H
class Timer
{
public:
Timer(); //Initialize the class
void start();
void stop();
void pause();
void unpause();
int get_ticks();
bool is_started();
bool is_paused();
private:
int startTicks; //The clock time when the time start
int pausedTicks; //The clock stored when the timer was paused
bool paused; // The timer status
bool started; // The timer status
}
#endif
//timer.cpp
#include "timer.h"
#include "SDL_image.h"
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;
}
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;
}
}
int Timer::get_ticks()
{
if(started == true)
{
if(paused == true)
return pausedTicks;
else
return SDL_GetTicks() - startTicks;
}
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
At the end, the Output says:
#5 Crossbones+ - Reputation: 1373
Posted 29 December 2012 - 03:19 PM
Try using Pre-Compiled headers. Saves you a lot of time
.
When you declare a variable in Main.cpp, it isn't included in any other files, and it's generally bad practice to include "Main.cpp" in another .h or .cpp file. What you could do is make a new file called "Global.h" and decalre all your Global variables in that file. That way, all you have to do is include "Global.h" in all of your source files.
Cheers
!
Edited by superman3275, 29 December 2012 - 03:20 PM.
Here's Breakout:
Breakout!
If you need some photo editing done, contact me:
superman3275@gmail.com
if you want some programming help, or are recruiting for a game development team, either PM me on here or email me up there
#6 Members - Reputation: 122
Posted 29 December 2012 - 05:23 PM
I followed your advice. Now the error its different and as always I'm clueless. Who else but Superman can help me
Here´s my source code.
Attached Files
#8 Crossbones+ - Reputation: 1373
Posted 29 December 2012 - 09:47 PM
Can you attach your source code? It's hard to understand the errors without context, however I believe your code is missing many Semicolons(;) after you are declaring Variables / Structures. You are also trying to convert (or indirectly converting) some of your variables into Integers.
Here's Breakout:
Breakout!
If you need some photo editing done, contact me:
superman3275@gmail.com
if you want some programming help, or are recruiting for a game development team, either PM me on here or email me up there
#9 Members - Reputation: 122
Posted 30 December 2012 - 02:22 PM
https://dl.dropbox.com/u/3679069/Pruebas%20con%20SDL.rar







