Page 1 of 1

Some Level Editor problems!

Posted: Mon Feb 16, 2009 12:21 pm
by KuramaYoko10
Soo, I started working on the level editor for my little rpg, and I got one weird issue :O

The thing is on the clipping part of the tilesheet, not the clipping itself but what happens between it and the rendering of the tilesheet!
I do the clipping and load the function on the OnInit() part, then when I try using the clipTiles[] on the OnRender(), or OnLogic() that is inside my game loop, but suddenly all the values of clipTiles[] are set to NULL o.O

Here are some of the source codes:

-first the main function and game loop:

Code: Select all

//main.cpp


#include "main.h"
//#include "Tile.h"





App::App()
{
	Running = true;

	screen = NULL;
	layout = NULL;

}


int App::OnExecute()
{
	if(OnInit() == false)
	{
		Running = false;
	}


	while(Running)
	{
		OnEvent();
		
		OnLogic();

		OnRender();
	}

	OnCleanup();


	return 0;
}


int main(int argc, char *argv[])
{
	App Execute;

	return Execute.OnExecute();
}

-Here is the source code of the tile part:

Code: Select all

//Tile.cpp

#include "Tile.h"
#include "Globals.h"
#include "Surface.h"





Tile::Tile()
{
	
}

//Dont care about this now
SDL_Surface* Tile::LoadTilesheet(char *filename)
{
	SDL_Surface *loaded = NULL;
	SDL_Surface *optmized = NULL;

	loaded = IMG_Load(filename);


	for(int a = 0; a < 66; a++)
	{
		SurfaceBlit::DrawClips(tileCell[a].x, tileCell[a].y, loaded, optmized, &clipTiles[a]);
	}
	
	return optmized;
}


void Tile::TileClips()
{
	int k = 0;
	int j = 0;
	int offX = 11;
	int offY = 620;

	for(int a = 0; a < 66; a++)
	{
		if(a == 22 || a == 44 || a == 66)
		{
			k = 0;
			j++;
		}

		clipTiles[ a ].x = TILE_WIDTH * k;
		clipTiles[ a ].y = TILE_HEIGHT * j;
		clipTiles[ a ].w = TILE_WIDTH;
		clipTiles[ a ].h = TILE_HEIGHT;
		tileCell[ a ].x = offX + (TILE_WIDTH * k);
		tileCell[ a ].y = offY + (TILE_HEIGHT * j);

		//printf("%d %d \n", tileCell[a].x, tileCell[a].y);

		k++;
	}
}
-Here is the OnInit() and OnRender()

Code: Select all

//OnInit.cpp

#include "main.h"
#include "Globals.h"
#include "Surface.h"
#include "Tile.h"







bool App::OnInit()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
	{
		return false;
	}

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);

	if(screen == NULL)
	{
		return false;
	}

	SDL_WM_SetCaption("The Game - Level Editor", NULL);

		
	Tile::TileClips();

	layout = SurfaceBlit::OnLoad("assets/layout.png");
	grid = SurfaceBlit::OnLoad("assets/grid.png");

	tileSheet = Tile::LoadTilesheet("assets/tilesheet_2.png");
	//tileSheet = SurfaceBlit::OnLoad("assets/tilesheet_2.png");


	return true;
}

Code: Select all

//OnRender.cpp

#include "main.h"
#include "Surface.h"
#include "Tile.h"




void App::OnRender()
{
	SurfaceBlit::DrawIMG(0, 0, layout, screen);
	SurfaceBlit::DrawIMG(10, 618, grid, screen);

	SurfaceBlit::DrawIMG(11, 620, tileSheet, screen);
	//SurfaceBlit::DrawClips(clipTiles[1].x, clipTiles[2].y, tileSheet, screen, &clipTiles[3]);

	SDL_Flip(screen);
}
Here is the thing... if I try printing the values of 'clipTile[5].x' on whatever part of OnInit() it will show right ... or if I try printing it inside my LoadTileSheet() it will also show the right numbers...
but if i try printing it on the OnRender() it will print '0' ... it does that in the transition between

Code: Select all

OnInit();

while(Running)
{
.
.
.
        OnRender();
}
I cant get to know 'why that???' ... I have already fried my neurons, can anyone help me solve this 'little' mistery?

P.S: And even if I try executing TileClips() in "OnRender()" and try printing the values after it, it will print "0" O.o

Re: Some Level Editor problems!

Posted: Mon Feb 16, 2009 12:35 pm
by Ginto8
Could you post the header files with your classes, as well as your LoadTileSheet() function? Those would greatly help with understanding what is wrong.

I may have overlooked something, seeing as I only skimmed your code, but seeing the other parts would greatly help. ;)

Re: Some Level Editor problems!

Posted: Mon Feb 16, 2009 12:49 pm
by KuramaYoko10
Ginto8 wrote:Could you post the header files with your classes, as well as your LoadTileSheet() function? Those would greatly help with understanding what is wrong.

I may have overlooked something, seeing as I only skimmed your code, but seeing the other parts would greatly help. ;)
Sure, but dont care about the LoadTileSheet() function I was going to implement it just to not have to put a 'for loop' inside the OnRender() and to do the loading of the tilesheet simpler but I am not sure it is going to work... so I could just do:

Code: Select all

SufaceBlit::OnLoad("assets/tilesheet_2.png");
  
for(int a = 0; a < 66; a++)
{
         SurfaceBlit::DrawClips(....);
}

Another thing, I just did a random trial and got another clue, if I try declaring a 'int variable' in the Tile.h and try printing it in the OnRender, its value will be printed as "0" too =/
But if i declare it inside the App structure, it will print the right value ... hmmm

I am still learning about classes and structs, and how to interact all of them together, so I dont know yet if this is my mistake!

Here is the headers:
Click here to see the hidden message (It might contain spoilers)

Code: Select all

//main.h

#ifndef _MAIN_H_
#define _MAIN_H_


#include "SDL.h"
#include "SDL_image.h"



struct App
{
	private:
		bool Running;

		SDL_Event event;

		SDL_Surface *screen;
		SDL_Surface *layout;
		SDL_Surface *grid;
		SDL_Surface *tileSheet;

	public:
		App();

		int OnExecute();
		bool OnInit();
		void OnEvent();
		void OnLogic();
		void OnRender();
		void OnCleanup();

};


#endif

Code: Select all

//Tile.h

#ifndef _TILE_H_
#define _TILE_H_


#include "SDL.h"
#include "SDL_image.h"
#include "Globals.h"



static SDL_Rect clipTiles[66];


struct Tile
{
	public:
		Tile();

		static void TileClips();
		static SDL_Surface* LoadTilesheet(char *filename);
		static void ApplyTile();

		int x;
		int y;


}static tileCell[66];



#endif

Code: Select all

//Surface.h

#ifndef _SURFACE_H_
#define _SURFACE_H_


#include "SDL.h"
#include "SDL_image.h"



struct SurfaceBlit
{
	public:
		SurfaceBlit();

		static SDL_Surface *OnLoad(char *file);

		static void DrawIMG(int x, int y, SDL_Surface *source, SDL_Surface *destination);
		static void DrawClips(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clips);
		
};


#endif


Hope you can find the mistery there ;)

Re: Some Level Editor problems!

Posted: Wed Feb 18, 2009 9:23 am
by KuramaYoko10
So... I got it solved...

First, I decided that I am going to make it full C++, because I know C (and like it), but at the same time I am trying to learn C, C++ classes and stuff appears in my code and try to convert it to C, and it all get mixed up and I thing it is not helping me any..

Now the structs are classes, the classes are 'friends' of each other so now I can access private variables from others, and it works like this :)

I am going to use this oportunity to start learning C++ and how C++ 'changed' the basic C, and how it does stuff different...
And I already got a question... when people say about OO design.. do they mean about a code made out of classes? (like my level editor here.. kinda going on this way)

:)

Re: Some Level Editor problems!

Posted: Wed Feb 18, 2009 12:02 pm
by programmerinprogress
classes are a part of object- oriented design...

As for making all classes friends of eachother, that sort of defeats the object of OO Design, the whole point is so that your program is made up of isolated objects which interact with eachother through accessor methods (get() and Set() type functions within classes)

Another principle is to keep to allow you to change your implementation, without constantly changing your interface.

That's the basic idea anyway, but of course you do what works best IMO, sometimes OO design is practical, sometimes there might be a much easier way of doing something that violates OO.

You're the programmer, you get to choose ;)

Re: Some Level Editor problems!

Posted: Wed Feb 18, 2009 2:06 pm
by KuramaYoko10
Ah I am starting to get it now xD...

The thing is... I have the SDL_Surfaces in my main class (App), and they are private, which means only this class members can manipulate those surfaces right?
But I had to let my Tile::SetTilesheet() function to draw stuff to one of those surfaces, and it first said: "cannot access non-static member", but if I make it static, I get linker errors, ... and even if i make the surfaces public it wouldnt work, so then I read about those friend classes which let a class to manipulate the private stuff of another, which worked for me...

I have 2 classes that are friends now 'Tile' and 'App' ... just to make it work this way, but since you have said that I will try avoiding it the next time =D
But in this case I believe that I am not letting my code so dirty right?


thanks for the tips ;)

Re: Some Level Editor problems!

Posted: Fri Feb 20, 2009 6:49 am
by programmerinprogress
your members may only be able to access the surface directly, but that doesn't mean you can write accessor functions that allow external sources to pass in data for that surface.

you could try writing an accessor function for App, such as App.SetNewSurface(TileSheet.GetTile());

it's hard to explain, but what i'm getting at is you could create accessor methods for both classes, so that they are never actually assigning variables in which they have no access to.

the main objective IMO is to make your data private, and your functions(some which require public scope) to be public, that way you are able to control access to your class, and your program doesn't go barmy because someone (or yourself) decides to do two different things with the same variable.