• Advertisement


  • Content count

  • Joined

  • Last visited

Community Reputation

105 Neutral

About stoffe1100

  • Rank
  1. SDL collision detection!

    [quote name='CableGuy' timestamp='1338661236' post='4945622'] I didn't quite get exactly what type of game you are creating and what exactly you are trying to achieve... If you can answer those question I might be able to help you rethink your problem [/quote] Hmm, I am actually just doing this to train myself on sdl. But if i say mario? something like that.
  2. SDL collision detection!

    [quote name='CableGuy' timestamp='1338644114' post='4945571'] What do you mean by strange? What are the exact problems you are facing? Tile collision is pretty easy. Usually you have a 2d array representing the map, checking for collison simply invovlves getting the player position in the tile map and indexing the array to check if this tile is passable. Your code looks a bit overly complicated. Why do you have a for loop when checking for collisions? [/quote] Actually my map is stored in a 2 dimensional vector called map. For the moment it works pretty well when colliding Up, Down and left. But i only get slowed when i collide from the right side. And if i push towards a wall i get pushed to the top of the wall without pressing up key. And sometimes i just fall under the whole map when colliding. What i am really looking for is like a whole new structure for how my collsion should look like. I dont understand why it is over complicated? please tell me and i will try to explain. Also when im colliding my ball is bouncing very quickly. Which it shouldnt because everything should be calculated and set back within one frame! Ive been playing around with some stuffs and i updated the player::move() function. When it is like this the collision in the Y axis is working great. but the X axis collisions are super weird. Please help me, i have no clue on how to continue. I cant stop thinking of those problems. its really anoying
  3. SDL collision detection!

    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 [CODE] #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 [/CODE] Here is game.cpp [CODE] #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; } [/CODE] Here is player.h [CODE] #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 [/CODE] here is player.cpp [CODE] #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); } [/CODE] Here is a class which contains the most basic stuffs that all classes need. its called base Here is base.h [CODE] #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 [/CODE] Here is base.cpp [CODE] #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) { } [/CODE]
  4. 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. [s]2. For some reason the ball dont colorkey.[/s] 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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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 [CODE] #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; } [/CODE] Here is paddle.h [CODE] #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; }; [/CODE] Here is paddle.cpp [CODE] #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; } [/CODE] Here is ball.h [CODE] #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; }; [/CODE] Here is ball.cpp [CODE] #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); } [/CODE]
  5. 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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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 [code]#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; } [/code] Here is NewButton.h [code]#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[/code] Here is NewButton.cpp [code]#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; }[/code]
  • Advertisement