Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Tallkotten

Member Since 28 Jun 2012
Offline Last Active Yesterday, 09:07 AM
-----

Topics I've Started

Switching rendering from SDL to OpenGL

08 January 2013 - 12:49 PM

Hi!

 

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

Hello!

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

Hello!

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)
	{
		itemVector.push_back(generateditem);
	}
	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++)
	{
		if(!itemMoved)
		{
			if(!(*it)->getFull())
			{
				(*it)->addItem(itemAdd);
				itemMoved = true;
			}
		}
	}
	return itemMoved;
}

Adds it to the content and changes some values
void Container::addItem(Item* itemAdd)
{
	if(content != NULL)
	{
		removeItem();
	}
	if(itemAdd != NULL)
	{
		content = itemAdd;
		content->setInCont(true);
		content->setX(contBox.x);
		content->setY(contBox.y);
		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;
	}
	else
	{
		  content->setX(contBox.x);
		  content->setY(contBox.y);
	}
}

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

Most memory effective way to do this

02 August 2012 - 06:15 AM

Hello!

I'm in need of help. I am about to build a system that displays info about an item on the screen and i need some help to understand which of 2 ways is the most memory effective.

So basically in my game i am making there are items on the ground. If the player holds down CTRL he/she gets up an UI that draws out the stats and name of the item.

I currently got a sloppy but functional system and i want to improve it. I got 2 ways in mind.

First way:

First create a object of the class in the initiation:
ItemInfo* UI = new ItemInfo();

And then when i want to draw something new i contact the build function in the class to update it.
UI->build(all the variables);

When i want to show it i switches a bool which makes it draw on the screen. I switch the bool to false when i want to hide it.

This way the UI is basically always gonna be on the screen (in the memory), just invisible to the user most times.

Second way:

When i want to draw the UI i create a new object of the class and sends all the necessary variables with it.
ItemInfo* UI = new ItemInfo(all the variables);

Then if i don't want to show it anymore i delete the object and set the pointer to NULL
delete UI;
UI = NULL;

If i want to show it again/update it i recreate it again, but with different variables. Of course if it isn't deleted already i have to do that first
ItemInfo* UI = new ItemInfo(all the variables);

I show it if the pointer != NULL, if it is NULL I don't try to draw it (since there isn't anything to draw)

This way is more memory and CPU intense when i am showing itemInfo but the memory is freed when i ain't doing anything with it.

Which way is more effective?

PARTNERS