Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 28 Jun 2012
Offline Last Active May 29 2013 02:15 PM

Topics I've Started

Uncertainty with VBO and OpenGL

19 May 2013 - 12:47 PM



I've spent the day trying to understand the use of VBO since i want to boost the FPS in my game.


However i'm having some trouble understanding the life-scope of the VBO and how to properly use it. Every sample code and tutorial i've found has shown a single static scene to render and I've got a pretty complex file architecture in my game. 




So this literally just occurred to me.

If a VBO behaves kind of like a texture (you generate and bind it) would i be correct to say that each entity in the game should hold it's own VBO?


For example; my player character would create a VBO at runtime and then call a render-function which fills it with data and renders it? 


The render code would take the VBO, texture and some placement information. Then my normal code would run (bind the texture, create a clip for the 2D image) then I create a new vertex buffer and a new texture buffer and bind them. Lastly the VBO should be bound and rendered.


A NPC would have it's own VBO just like the player does.



Am i thinking right? And even if i am, could you be so kind to explain the scope and use of a VBO? Like i said, all the examples just handled a static scene to render so i'm having trouble understanding how to update it.


My game is in 2D and currently renders everything as quads or circles (all collision is done with rectangles). The size of an object is never changed after creation. When an object animates it'll change the location of the clip within it's loaded imagesheet. So the image itself will rarely change.


I'm kind of tired at the moment so i hope i made myself clear enough for you to understand.


Looking forward to some answer, thanks in advance!

Switching rendering from SDL to OpenGL

08 January 2013 - 12:49 PM



I took it upon myself to finally start migrating from SDL to OpenGl in my project today. So far I've managed to (in isolated code) translate an SDL_Surface to an OpenGl texture which i then use to apply on polygons.


In my previous rendering method i used a lot of clips. Meaning I've got one image with maybe 3-5 images on it which i then clip before i render to get the desired image to show. I've managed to translate this over to OpenGl as well but it takes a bit of work and i need to know the loaded image dimensions before i do.


Just to clarify how the rendering process works now, so you get a better scope of my problem:


1. Loads image to an SDL_Surface

2. Saves it to an array

3. Something fetches the image memory location from the array

4. Converts the SDL_Surface to a OpenGl texture

5. Clips the texture

6. Renders it to QUADS

7. Destroys the texture


My Convert code is basically the same as this one here: http://content.gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL


Question time: 


1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.


2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png


3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?


4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?


I think that's all! Thanks for your time, i really appreciate answers!


// Tallkotten



Memory management, mainly using SDL

06 November 2012 - 05:24 AM


I'm currently trying to reduce the memory leak my program is causing and the process has me kind of dumbfounded.

I understand that everything you create using "new" you have to delete eventually so that there is no memory leak. The same goes for temporary pointers (right)?

Anyway I've located a part of my code that is leaking, to no surprise it's where i update one of the UI elements in my game. I'm going to simplify the code here because the question isn't about only that code but rather how i handle a situation like this (i have loads of them in my code at the moment).

So here goes:
void update(int timer)
	 //this is my update function which updates this certain element
	 //i have a reference to my loader class already loaded in, it's called imageLoader in this example. imageLoader stores all of the loaded
	 //images and can return a pointer to a certain image on function calls.

	 SDL_Surface* tempSurface = imageLoader.loadImg("example_image.png");
	 //tempSurface now stores a pointer to the image that is returned from loadImg();

	 SDL_BlirSurface(tempSurface, NULL, screen, NULL);
	 //now I've used the img and the local tempSurface is useless

	 //Now, if tempSurface only was a int* i could have just ignored it since it's just a pointer (right)?
	 //but as i understands it a SDL_Surface creates other pointers within itself that must be destroyed, and if i call
	 //SDL_FreeSurface(tempSurface); i will destroy the surface that it's pointing to, creating problems for other classes.
	 //memory is leaking here, and my guess is on the "tempSurface" which i am unable to destroy without killing it's reference!!!

How do i make sure i free all the memory possible without corrupting/deleting the reference?

I want to make sure that i have this same problem with TTF_Font, which is part of a "sub-library" to SDL.

I really appreciate any help i can get! :)

Deriving but keeping data

20 August 2012 - 05:15 AM


About a week ago i posted on this forum asking for help with a pointer problem. People were very helpful and gave me loads of tips to read up on.

So this week i've been reading like crazy and i am ready to basically re-write my project. I still doesn't know how to solve one thing thought.

At the moment i have a loader class which basically loads in images, fonts and eventually sounds. Every class is derived from this one. But as someone pointed out in the previous thread that must mean that all the files are loaded several times since the actual loading takes place in the constructor.

So for every class that's being made it must contact that constructor again and re-load everything.

I've gotten some tips from my dad and one was to make a static function and variable of the class to make sure it only loads once. He showed me an example of how to make it work. I'm just wondering how you guys tackle this problem and if static is the way to go or if there are several ways to do this!

Thank in advance!

Pretty advanced pointer problem

12 August 2012 - 12:43 PM

Hello again Gamedev forum!

I'm back with a mind twister. I've sat with this for a few hours and haven't moved an inch forward. I am beaten.

The problem is with a pointer. I've "watched" it while debugging to follow it. And basically this is how it goes.

Object created, pointer is correct -> sends it around abit until finally arrived in Class "Content" -> pointer is correct -> Assign new pointer the same value -> Still correct -> Adjust some values in the object with help from pointer -> Goes back to gameLoop -> back into Content class -> update function running -> Program crashes

The reason why it crashes is because it tries to update the same values it changed earlier with the pointer stored in the class. The pointer has now changed from being a pointer to a Class to being a pointer to the Classes Interface which no real code in it.

And the annoying thing is that I've got 3 different classes that uses the same interface and the error only occurs with this newly created class, ergo I've must have screwed something up. But i can for the love of god not locate it.

I'm going to post the code to these steps now as well as upload the project in case someone wants to try to debug it on his/her own.

Object created, with the createGold function:
int gold = 20*level*(diff/2) + (rand() % 20*level*2 - 10*level);
	if(gold > 0)
		lootContVector.at(vectorsize-1)->moveToBag(createGold(x, y, gold));

Item* itemMngr::createGold(int x, int y, int amount)
	Item* generateditem = NULL;
	generateditem = new Currency(x, y, amount);
	if(generateditem != NULL)
	return generateditem;

Sends it forward to a newly created lootcontainer's function
bool LootContainer::moveToBag(Item* itemAdd)
	bool itemMoved = false;
	for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
				itemMoved = true;
	return itemMoved;

Adds it to the content and changes some values
void Container::addItem(Item* itemAdd)
	if(content != NULL)
	if(itemAdd != NULL)
		content = itemAdd;
		full = true;

Goes back to the gameloop which sends an update command that eventually reaches the Container class within lootcontainer
void Container::update()
	if(content == NULL)
		full = false;

This code is where it crashes: "content->setX(contBox.x);". If i comment that out it will instead crash on the render function. The reason is that the pointer is no longer (Currency*) but (class Item*) in the "watches window".

Another weird thing it that is does work sometimes, if you are quick enough to spawn the loot... but only once.

If you want to download this yourself you should know that it is my first project and probably has a lot of sloppy code in it. To make the error occur you must spawn a Chest by pressing "R" and open it up.

Greatly appreciates help Posted Image

EDIT: The attached file does not come with any DLL's... i am running all the SDL DLL's and by adding these files to the project folder you should be able to run it: https://www.dropbox.com/s/p8jz7na8yodojwc/0.0.0080.rar