Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


stoffe1100

Member Since 02 Apr 2012
Offline Last Active Jun 03 2012 05:24 AM

Topics I've Started

SDL collision detection!

01 June 2012 - 02:07 PM

Hello, i am trying to make some basic game that are using tiles. Until now it has gone pretty well and forward. But now im just totally stucked. I am trying to write a move funtion to move the player, and after that it checks if there is a collsion and if there is it moves the player back. It works at some parts. But generally it is acting very strange. Could you please take a look at it?

Thanks in advice. I will send all the code i think is necessary and just reply if you need more.

If there is anything else that looks strange just tell me, i want to learn as much as possible.

Here is game.h

#include "base.h"
#include "player.h"
#ifndef GAME_H
#define GAME_H
class game : public base
{
public:
game(void);
~game(void);
private:
bool InitSDL();
void GameLoop();
void HandleEvents();
void UpdateGame();
void Render();
void LoadMap(string filename);
void BlitMap();
bool running;
bool fullscreen;
SDL_Event event;
bool keys[400];
player * Player;
SDL_Surface * screen,*Background,*Blocks;
SDL_Rect BackgroundBox;
vector< vector<int> > map;
};
#endif

Here is game.cpp

#include "game.h"
SDL_Rect base::coord;
game::game(void)
{
if(!this->InitSDL()){exit(1);}
BackgroundBox.x = BackgroundBox.y = coord.x = coord.y = 0;
coord.w = BackgroundBox.w = 800;
coord.h = BackgroundBox.h = 600;
fullscreen = false;
Background = Load_Image("Background.png");
Blocks = Load_Image("Blocks.png");
this->LoadMap("test.TILEMAP");
Player = new player;
Player->CurrentMap(map);
this->GameLoop();
}
void game::GameLoop()
{
Uint32 start, end;
running = true;
while(running)
{
  start = SDL_GetTicks();
  this->HandleEvents();
  this->UpdateGame();
  this->Render();
  end = SDL_GetTicks();
  if(1000/FPS > end - start)
  {
   SDL_Delay(1000/FPS - (end - start));
  }
}
}
void game::HandleEvents()
{
while(SDL_PollEvent(&event))
{
  switch(event.type)
  {
  case SDL_QUIT:
   running = false;
   break;
  case SDL_KEYDOWN:
   keys[event.key.keysym.sym] = true;
   break;
  case SDL_KEYUP:
   keys[event.key.keysym.sym] = false;
   break;
  }
}
}
void game::UpdateGame()
{
Player->SetXvel(0);
Player->SetYvel(0);
if(keys[SDLK_a]){
  coord.x-=vel;
  BackgroundBox.x-=vel;
  Player->SetXvel(-3);
  if(BackgroundBox.x <= 0)
  {
   BackgroundBox.x = Background->w - SCREEN_WIDTH;
  }
}
else if(keys[SDLK_d]){
  coord.x+=vel;
  BackgroundBox.x+=vel;
  Player->SetXvel(3);
  if(BackgroundBox.x >= Background->w - SCREEN_WIDTH)
  {
   BackgroundBox.x = 0;
  }
}
if(keys[SDLK_w]){
  Player->SetYvel(-3);
}else if(keys[SDLK_s]){
  Player->SetYvel(3);
}
else if(keys[SDLK_ESCAPE]){
  running = false;
}else if(keys[SDLK_F5]){
  fullscreen = (!fullscreen);
  if(fullscreen)
  {
   screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
  }else{
   screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
  }
}
Player->MovePlayer();

}
void game::Render()
{
SDL_BlitSurface(Background,&BackgroundBox,screen,NULL);
this->BlitMap();
Player->Blit(screen);
SDL_Flip(screen);
}
void game::LoadMap(string filename)
{
ifstream in(filename.c_str());
int Width,Height,current;
in >> Width >> Height;
if(!in.is_open())
{
  cout << "Error: could not load "<< filename << endl;
  return;
}
for(int j = 0;j != Height;j++)
{
  vector<int> TempVec;
  for(int k = 0; k != Width;k++)
  {
   if(in.eof())
   {
	cout << "Error: File end reached to early" << endl;
	return;
   }
   in >> current;
   TempVec.push_back(current);
  }
  map.push_back(TempVec);
}
in.close();
}
void game::BlitMap()
{
int start = (coord.x - coord.x % TILE_SIZE)/TILE_SIZE;
if(start < 0){start = 0;}
int end = ((coord.x + coord.w - coord.x % TILE_SIZE)/TILE_SIZE);
if(end > map[0].size()){end = map[0].size();}
for(int j = 0; j != map.size();j++)
{
  for(int k = start; k != end; k++)
  {
   SDL_Rect BlockRect = {(map[j][k]-1)*TILE_SIZE,0,TILE_SIZE,TILE_SIZE};
   SDL_Rect DestRect = {k * TILE_SIZE - coord.x,j*TILE_SIZE,TILE_SIZE,TILE_SIZE};
   SDL_BlitSurface(Blocks,&BlockRect,screen,&DestRect);
  }
}
}
game::~game(void)
{
delete Player;
SDL_FreeSurface(Blocks);
SDL_FreeSurface(Background);
SDL_Quit();
}
bool game::InitSDL()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
  cout << "Error: could not initialize SDL" << endl;
  return false;
}else{
  cout << "SDL initialized!" << endl;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL)
{
  cout << "Error: could not initialize the screen" << endl;
  return false;
}else{
  cout << "screen initialized!" << endl;
}
if(TTF_Init() == -1)
{
  cout << "Error: could not initialize TTF" << endl;
  return false;
}else{
  cout << "TTF initialized" << endl;
}
for(int i = 0; i != 400; i++)
{
  keys[i] = false;
}
return true;
}

Here is player.h

#include "base.h"
#ifndef PLAYER_H
#define PLAYER_H
class player : public base
{
public:
player(void);
void SetXvel(int vel);
void SetYvel(int vel);
void CurrentMap(vector<vector<int> > Map);

void MovePlayer();
void Blit(SDL_Surface * screen);
~player(void);
private:
vector< vector<int> > map;
SDL_Surface * image;
SDL_Rect box;
int Xvel,Yvel;
bool Ground;

};
#endif

here is player.cpp

#include "player.h"

player::player(void):
Yvel(3),
Xvel(5)
{
image = Load_Image("Player.png");
SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format,200,0,200));
box.w = image->w;
box.h = image->h;
box.x = SCREEN_WIDTH/2 - box.w/2;
box.y = SCREEN_HEIGHT/2;
Ground = false;
}
void player::SetXvel(int vel)
{
Xvel = vel;
}
void player::SetYvel(int vel)
{
Yvel = vel;
}
void player::MovePlayer()
{
box.x += Xvel;
box.y += Yvel;
int start = (coord.x - coord.x % TILE_SIZE)/TILE_SIZE;
if(start < 0){start = 0;}
int end = ((coord.x + coord.w - coord.x % TILE_SIZE)/TILE_SIZE);
if(end > map[0].size()){end = map[0].size();}
for(int j = 0; j != map.size();j++)
{
  for(int k = start; k != end; k++)
  {
   if(map[j][k] == 0)
   {
	continue;
   }
   SDL_Rect DestRect = {k * TILE_SIZE - coord.x,j*TILE_SIZE,TILE_SIZE,TILE_SIZE};
   if(CheckCollision(box,DestRect))
   {
    if(box.x+box.w >= DestRect.x)
	 {
	 box.x-=Xvel;
	 }
	 else if(box.x <= DestRect.x + DestRect.w)
	 {
	 box.x-=Xvel;
	 }
	 if(box.y+box.h >= DestRect.y)
	 {
	 box.y-=Yvel;
	 } 
	 else if(box.y <= DestRect.y + DestRect.h)
	 {
	 box.y-=Yvel;
	 }
   }
  }
}
}
void player::CurrentMap(vector<vector<int> > Map)
{
map = Map;
}
void player::Blit(SDL_Surface * screen)
{
SDL_BlitSurface(image,NULL,screen,&box);
}
player::~player(void)
{
SDL_FreeSurface(image);
}

Here is a class which contains the most basic stuffs that all classes need. its called base

Here is base.h

#ifndef BASE_H
#define BASE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
using namespace std;
class base
{
public:
static const int SCREEN_WIDTH = 800;
static const int SCREEN_HEIGHT = 600;
static const int SCREEN_BPP = 32;
static const int FPS = 60;

static const int Gravity = 2;
static const int vel = 2;
static const int TILE_SIZE = 50;
static SDL_Rect coord;
base(void);
SDL_Surface * Load_Image(string filename);
bool CheckCollision(SDL_Rect r1,SDL_Rect r2);
~base(void);
};
#endif

Here is base.cpp

#include "base.h"

base::base(void)
{
}
SDL_Surface * base::Load_Image(string filename)
{
SDL_Surface * IMG = IMG_Load(filename.c_str());
if(IMG != NULL)
{
  SDL_Surface * Opt = SDL_DisplayFormat(IMG);
  SDL_FreeSurface(IMG);
  cout << "Loading image " << filename << " Succeeded!" << endl;
  return Opt;
}else{
  cout << "Error: image " << filename << " could not be loaded" << endl;
  return NULL;
}
}
bool base::CheckCollision(SDL_Rect r1,SDL_Rect r2)
{
if(r1.x > r2.x+r2.w){
  return false;
}
else if(r1.y > r2.y+r2.h){
  return false;
}
else if(r1.x+r1.w < r2.x){
  return false;
}
else if(r1.y+r1.h < r2.y){
  return false;
}else{
  return true;
}
}
base::~base(void)
{
}

some SDL pong problems!

04 May 2012 - 05:34 PM

Hello!

I have tried to make my first pong game in c++ with sdl. I can play it now, but i got a few problems that i have no idea on how to solve.

I have googled around on it but i find nothing useful for me.

So here is the problems this far,

1. When i enter fullscreen the screen flickers so much you cant see what is going on.
2. For some reason the ball dont colorkey.
3. I can´t hold down keys which is strange since i have actually made a bool array for the keys

I wanna say it before you complain, Im really bad at english so please do not do uneccesary comments on that.

Im new to sdl so if you got constructive critic please leave a comment and help me get better Posted Image
I know the code got a few memory leaks and bad code but i will fix it when i get the game to work properly first.

Thank you in advance!

So here comes the code. Posted Image

global_functions is just a class with some handy functions which felt uneccesary to post here because the post will get even larger.

Here is main.cpp

#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_Image.h>
#include "paddle.h"
#include "ball.h"
#define FPS 30
using namespace std;
SDL_Surface * screen;
SDL_Event event;
Uint32 start;
bool keys[274] = {false};
bool fullscreen = false;
bool running = true;
bool init();
int main(int argc,char** argv)
{
if((!init())){SDL_Quit(); return 0;}
paddle paddle1(1);
paddle paddle2(2);
ball Ball;
while(running)
{
  start = SDL_GetTicks();
  while(SDL_PollEvent(&event))
  {
   switch(event.type)
   {
   case SDL_QUIT:
	running = false;
	break;
   case SDL_KEYUP:
	keys[event.key.keysym.sym] = false;
	break;
   case SDL_KEYDOWN:
	if(event.key.keysym.sym == SDLK_f){fullscreen = (!fullscreen);}
	if(event.key.keysym.sym == SDLK_ESCAPE){running = false; break;}
	keys[event.key.keysym.sym] = true;
	break;
   }
  }
  if(keys[SDLK_w]){cout << "W IS DOWN" << endl; paddle1.Up();}
  if(keys[SDLK_s]){cout << "S IS DOWN" << endl; paddle1.Down();}
  if(keys[SDLK_UP]){cout << "ARROWUP IS DOWN" << endl;paddle2.Up();}
  if(keys[SDLK_DOWN]){cout << "ARROWDOWN IS DOWN" << endl; paddle2.Down();}
  if(fullscreen){screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);}else{screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);}
  SDL_FillRect(screen,NULL,NULL);
  paddle1.Blit();
  paddle2.Blit();
  Ball.update(paddle1.ReturnRect(),paddle2.ReturnRect());
  SDL_Flip(screen);
  if(1000/FPS > SDL_GetTicks() - start){SDL_Delay(1000/FPS - (SDL_GetTicks() - start));}
}
SDL_Quit();
return 0;
}
bool init(){
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
  std::cout << "Could not initialize sdl" << std::endl;
  return false;
}
std::cout << "SDL initilized" << std::endl;
screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL){
  std::cout << "Could not setup screen" << std::endl;
  return false;
}
std::cout << "Screen is setup" << std::endl;

if(TTF_Init() == -1){
  std::cout << "Could not initialize ttf" << std::endl;
  return false;
}
std::cout << "TTF initilized" << std::endl;
SDL_WM_SetCaption("Pong",NULL);
return true;
}

Here is paddle.h

#pragma once
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_Image.h>
#include "global_functions.h"

class paddle
{
public:
paddle(short nr);
~paddle(void);
void Up();
void Down();
void Blit();
SDL_Rect ReturnRect();

private:
int YVel;
SDL_Surface * image;
SDL_Rect box;
};

Here is paddle.cpp

#include "paddle.h"

paddle::paddle(short nr)
{
global_functions f;
image = f.Load_Image("paddle.jpg");
image->refcount++;
YVel = 50;
box.w = image->w;
box.h = image->h;
box.y = SDL_GetVideoSurface()->h/2 - box.h/2;
switch(nr)
{
case 1:
  box.x = 0;
  break;
case 2:
  box.x = SDL_GetVideoSurface()->w - box.w;
  break;
}
}
void paddle::Up()
{
box.y-=YVel;
}
void paddle::Down()
{
box.y+=YVel;
if(box.y + box.h >= SDL_GetVideoSurface()->h)
{
  box.y = SDL_GetVideoSurface()->h - box.h;
}
}
void paddle::Blit()
{
SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&box);
}
paddle::~paddle(void)
{
SDL_FreeSurface(image);
}
SDL_Rect paddle::ReturnRect()
{
return box;
}

Here is ball.h

#pragma once
#include <SDL.h>
#include "global_functions.h"
class ball
{
public:
ball(void);
void update(SDL_Rect PaddleRect,SDL_Rect PaddleRect2);
~ball(void);
private:
SDL_Rect box;
int XVel,YVel;
SDL_Surface * image;
};

Here is ball.cpp

#include "ball.h"

ball::ball(void)
{
global_functions f;
image = f.Load_Image("ball.bmp");
image->refcount++;
SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format,200,0,200));
YVel = 5;
XVel = 5;
box.w = image->w;
box.h = image->h;
box.x = SDL_GetVideoSurface()->w/2 - box.w/2;
box.y = SDL_GetVideoSurface()->h/2 - box.h/2;
}
void ball::update(SDL_Rect PaddleRect1,SDL_Rect PaddleRect2)
{
global_functions f;
box.x+=XVel;
box.y+=YVel;
if(f.CheckCollision(box,PaddleRect1)||f.CheckCollision(box,PaddleRect2)) {XVel = -XVel;}
if((box.y+box.h) >= SDL_GetVideoSurface()->h || box.y <= 0){YVel = -YVel;}
SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&box);
}
ball::~ball(void)
{
SDL_FreeSurface(image);
}

c++ SDL Button class

02 April 2012 - 06:03 PM

Hello! Im new at SDL. I´m doing small experiments like what i was trying to do now when i got a stuck on a problem. I have made a button class called NewButton that got a bool function called CheckEvents that is supposed to return true if left mouse button was pressed while the mouse arrow is inside the "ButtonRect". It compiles fine and i can see my button on the screen. But nothing happends when i press it.

I know the code is not completely object oriented but that is not what i am focusing on now

And by the way, if you find any other things that i should have i mind please tell me, i wanna learn as much as possible Posted Image

And another question while i'm anyway starting a thread. Do i really have to pass in the screen surface to the button. Are there no way to make the screen variable global somehow. Seems dumb to do it this way.

I have been sitting up half night now only looking at this....

Thanks in advance!



Here is the main.cpp

#include <sdl.h>
#include <string>
#include "NewButton.h"
SDL_Event event;
bool running = true;
SDL_Surface * screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);

int main(int argc, char * args[]){
SDL_Surface * Image = SDL_LoadBMP("TestKnapp.BMP");
Image = SDL_DisplayFormat(Image);
NewButton ExitButton(400,300,100,50,Image,screen);
while(running){
  if(SDL_PollEvent(&event)){
  
   if(event.type == SDL_QUIT){running = false; SDL_Quit(); break;}
   if(event.type == SDL_KEYDOWN){
	if(event.key.keysym.sym == SDLK_ESCAPE){running = false; SDL_Quit(); break;}
   }
   if(ExitButton.CheckEvents()){
	running = false;
	SDL_Quit();
	break;
   }
  }
  ExitButton.DrawToScreen();
  SDL_Flip(screen);
}
return 0;
}

Here is NewButton.h

#pragma once
#ifndef newbutton
#define newbutton
#include <SDL.h>
#include <string>
using namespace std;

class NewButton
{
public:
NewButton(int x,int y, int w, int h,SDL_Surface * Image,SDL_Surface * screen);
void DrawToScreen();
bool CheckEvents();

private:
SDL_Surface * LocalScreen;
SDL_Surface * ButtonImage;
SDL_Rect ButtonRect;
SDL_Event event;
int MouseX,MouseY;
};

#endif

Here is NewButton.cpp

#include "NewButton.h"
#include <SDL.h>
#include <string>
using namespace std;
NewButton::NewButton(int x,int y, int w, int h,SDL_Surface * Image,SDL_Surface * screen):
LocalScreen(screen),
MouseX(0),
MouseY(0)
{
ButtonImage = Image;
ButtonRect.x = x;
ButtonRect.y = y;
ButtonRect.w = w;
ButtonRect.h = h;
}
void NewButton::DrawToScreen(){
SDL_BlitSurface(ButtonImage,NULL,LocalScreen,&ButtonRect);
}
bool NewButton::CheckEvents(){
if(SDL_PollEvent(&event)){
  if(event.type == SDL_MOUSEBUTTONDOWN){
   if(event.button.button == SDL_BUTTON_LEFT){
	MouseX = event.button.x;
	MouseY = event.button.y;
	if((MouseX > ButtonRect.x) && (MouseY > ButtonRect.y) && (MouseX < (ButtonRect.x + ButtonRect.w)) && (MouseY < (ButtonRect.y + ButtonRect.h))){
	 return true;
	}
   }
  }
}


return false;
}

PARTNERS