Jump to content

  • Log In with Google      Sign In   
  • Create Account


some SDL pong problems!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 stoffe1100   Members   -  Reputation: 105

Like
0Likes
Like

Posted 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);
}

Edited by stoffe1100, 05 May 2012 - 08:51 AM.


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS