Jump to content

  • Log In with Google      Sign In   
  • Create Account


_Zac_

Member Since 26 May 2008
Offline Last Active Jan 08 2012 06:15 AM

Topics I've Started

[SDL] Frame Limiting

04 January 2012 - 01:33 AM

Alright, I know Frame Limiting is something that's brought up a lot, but my question is how do I do it accurately.

I've noticed that my display surface somehow never gets double-buffered, so I'm trying to find another way. Considering that 1000/60 is a decimal while SDL_GetTicks() is an integer, it's extremely hard to get an accurate 60 FPS.

So far I've tried waiting for 16ms one frame, then 17ms the next two and just repeating this cycle (updating the average every 3 frames of course). (16+17+17)/3=16.666... but my FPS calculator keeps detecting 60.049 FPS this way.

I've also tried creating a float and after waiting the desired amount of milliseconds doing while((Uint32)NextTick<=SDL_GetTicks())NextTick+=1000.0f/60.0f; Usually with the same results but with a less stable framerate.

So either my FPS counter is wrong and I'm getting 60 FPS (sometimes dipping) or I can't really get more accurate than 60.049 FPS +or-0.1

What would be the most accurate way to achieve 60 FPS without double buffering?

[C++] Collision Handling

30 September 2011 - 08:45 PM

I guess a few things to start off with, I'm using C++.

Alright, I have this class called ObjectHandler, which pretty much handles all the game objects. A game Object is defined as anything with a loop() and render() function as well as collision functions. The ObjectHandler stores all the Objects in a vector that when you call the ObjectHandler's loop() or render() functions it then calls every Objects loop() or render() functions.

Now, when calling the loop command the ObjectHandler gives itself to the object (like calling Object.loop(ObjectHandler this) so that the Object can use the ObjectHandler to check collisions with all other objects. The problem here is that if I call the ObjectHandler for a collision check I cannot check for specific subclasses of Object without creating separate functions and separate vectors.

So for a gist here's some psuedocode for what I want to do:


bool ObjectHandler::collision(class x)

{

    for(int i=0;i<ObjVect.size;i++)

    {

        if(ObjVect[i] could be considered an x && ObjVect[i]->collision())

        {

        return true;

        }

    }

return false;

}


Now of course collision would have a few more variables to pass in, but I believe this gets the point across of what I want to do.

Now I hear there is an easy way of doing this in C#, but I also hear that if you ever have to call a function to tell if a certain object is a certain class then you have design flaws. So I'm wondering if doing it like this is a major flaw in design and would be greatful if you could point me towards some faqs or give some advice if you believe that is the case.

[SDL] SDL_Rect question

25 September 2011 - 02:42 PM

If I put in a rectangle that has x and y equal 0, and the width and height equal to 32, and then blit it, would it draw a rectangle from 0 to 31 x and y? (So top-left corner is at (0,0) and the bottom-right corner is at (31,31))

Thanks, I need to know this because I created a collision-detection function that takes in a rectangle's x,y,width and height and that one pixel could make all the difference.


"Game" crashes when instantiating object

22 September 2011 - 04:53 PM

#ifndef _GAME_H_
#define _GAME_H_

#include "State.h"
#include "CEvent.h"
#include "GSurface.h"
#include <iostream>

class Game:public CEvent
{
private:
bool Running;
SDL_Surface* SurfDisplay;
State* CurrentState;
GSurface* temp;
SDL_Surface* temp2;

public:
Game();
~Game();
int onExecute();
bool onInit(); //inits display surface
void onEvent(SDL_Event* event); //Handles button presses
void onLoop(); //Game Loop
void onRender(); //Graphics
void onCleanup(); //Cleans up entire game
void onExit(); //Sets Running to false
void OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode);
};

Game::Game()
{
SDL_Init(SDL_INIT_EVERYTHING);
Running=true;
SurfDisplay=0;
CurrentState=0;
temp=0;
//temp=new GSurface("image0","./image.png");
temp2=0;

}

bool Game::onInit()
{
//if(SDL_Init(SDL_INIT_EVERYTHING)<0){return false;}
SurfDisplay=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(SurfDisplay==NULL)
{
return false;
}
temp=new GSurface("image0","./image.png");
temp2=CSurface::OnLoad("./image.png");
return true;
}

void Game::onRender()
{
if(temp->drawSurface(SurfDisplay,32,32)==false)
{
Running=false;
}
//CSurface::OnDraw(SurfDisplay,temp2,0,0);

SDL_Flip(SurfDisplay);
}

The previous problem I was having has been fixed, but now I have a new problem, though it still concerns strings. The function OnLoad() takes in a const* char and returns a pointer to an SDL_Surface. The variable File is a std::string equal to "./image.png". I put in File.c_str() but it fails to load. When I put in "./image.png" instead it works. I wish to know what I am doing wrong and what I can do to solve this.
#ifndef _GSURFACE_H_
#define _GSURFACE_H_

#include "CSurface.h"
#include <iostream>

class GSurface
{
public:
std::string File;
private:
SDL_Surface* Surface;
std::string Name;
//std::string File;
bool Loaded;
Uint8 R,G,B,A;
int Width,Height;
int iWidth,iHeight;
int ImageMax;

public:
GSurface(std::string n,std::string location);
GSurface(std::string n,std::string location,int iw,int ih);
GSurface(std::string n,std::string location,int iw,int ih,Uint8 red,Uint8 

green,Uint8 blue,Uint8 alpha);
//GSurface(char* n,char* location,Uint32 RGBA);
~GSurface();
bool drawSurface(SDL_Surface* surfDest,int x,int y,int ix,int iy,int w,int h);
bool drawSurface(SDL_Surface* surfDest,int x,int y,int image);
bool drawSurface(SDL_Surface* surfDest,int x,int y);
bool setAlpha(Uint8 a); //Sets alpha of surface
void setColorKey(Uint8 r,Uint8 g,Uint8 b,Uint8 a); //Sets color key and alpha
void setImage(int w,int h);
//bool setColorKey(Uint32 rgba);
std::string getName();
void setName(std::string n);
void free(); //calls unload()

private:
bool load();
void unload();
void checkLoaded();
};

GSurface::GSurface(std::string n,std::string location)
{
GSurface(n,location,NULL,NULL,NULL,NULL,NULL,NULL);
}
GSurface::GSurface(std::string n,std::string location,int iw,int ih)
{
GSurface(n,location,iw,ih,NULL,NULL,NULL,NULL);
}
GSurface::GSurface(std::string n,std::string location,int iw,int ih,Uint8 

red,Uint8 green,Uint8 blue,Uint8 alpha)
{
Name=n;
File=location;
R=red;
G=green;
B=blue;
A=alpha;
iWidth=iw;
iHeight=ih;

Surface=NULL;
Loaded=false;
Width=Height=ImageMax=0;
}
GSurface::~GSurface()
{
unload();
Surface=NULL;
}
bool GSurface::load()
{
if(Loaded==true)
{
return true;
}
  //Surface=CSurface::OnLoad(File.c_str()); NOTE: Does Not Work
  Surface=CSurface::OnLoad("./image.png"); //But if replaced with this it does 

work
if(Surface==NULL)
{
return false;
}
Loaded=true;
if(Width==0)
{
Width=Surface->w;
Height=Surface->h;
}
if(R!=NULL&&G!=NULL&&B!=NULL&&A!=NULL)
{
CSurface::OnColorKey(Surface,R,G,B);
}
return true;
}
void GSurface::unload()
{
SDL_FreeSurface(Surface);
Surface=NULL;
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y)
{
checkLoaded();if(!Loaded){return false;}

return CSurface::OnDraw(surfDest,Surface,x,y);
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y,int ix,int iy,int 

w,int h)
{
checkLoaded();if(!Loaded){return false;}

return CSurface::OnDraw(surfDest,Surface,x,y,ix,iy,w,h);
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y,int image)
{
checkLoaded();if(!Loaded){return false;}

int ix,iy,fw;

while(image>ImageMax)
{
image-=ImageMax;
}

fw=(int)(Width/iWidth);
ix=(image%fw)*iWidth;
iy=(image/fw)*iHeight;

return CSurface::OnDraw(surfDest,Surface,x,y,ix,iy,iWidth,iHeight);
}
void GSurface::setColorKey(Uint8 r,Uint8 g,Uint8 b,Uint8 a)
{
R=r;
G=g;
B=b;
A=a;
if(Loaded==true)
{
CSurface::OnColorKey(Surface,R,G,B);
}
}
void GSurface::setImage(int w,int h)
{
iWidth=w;
iHeight=h;
}
void GSurface::checkLoaded()
{
if(!Loaded&&load())
{
Loaded=true;
}
}
std::string GSurface::getName()
{
return Name;
}
void GSurface::setName(std::string n)
{
Name=n;
}
void GSurface::free()
{
unload();
}

#endif

SDL_Surface* CSurface::OnLoad(const char* File)
{
    SDL_Surface* Surf_Temp=NULL;
    SDL_Surface* Surf_Return=NULL;

    if((Surf_Temp=IMG_Load(File))==NULL)
    {
        return NULL;
    }
    Surf_Return=SDL_DisplayFormat(Surf_Temp);
    SDL_FreeSurface(Surf_Temp);
    return Surf_Return;
}

Hiding Resources in .DAT files

14 August 2011 - 07:36 PM

When most games are released you'll usually never find the resources (images, sounds, etc.) just out in the open when searching through the folders. When I looked at a Doujin game I downloaded they were all in a .dat file that was next to the original .exe. There was no easy way for me to extract the information from these files.

I was wondering how I could implement this. Are they like folder files or zip files? (I really don't know more about this topic other than "all the resources are packed into these .dat files and I want to know how to do the same".)


PARTNERS