• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
stoffe1100

some SDL pong problems!

0 posts in this topic

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] Edited by stoffe1100
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0