Page 1 of 1

Problems with my engine!!

Posted: Tue Feb 10, 2009 9:38 am
by KuramaYoko10
I have started working on a simple game engine, since I have finally finished my first game (The Space Invaders one =D),which will be used on simple RPG at first... but I am having some issues on it!! :(


It was created based on a tutorial that I read on a website, that works using C and SDL, and it has the following characteristics:
- core.h : declares my Engine struct, which will set the basic functions to the game loop
- core.cpp : runs the main() and the game loop

Code: Select all

//core.cpp

#include "core.h"



Engine::Engine()
{
	Running = true;

}

int Engine::OnExecute()
{
	if(OnInit() == false)
	{
		return -1;
	}

	SDL_Event event;

	while(Running)
	{
		while(SDL_PollEvent(&event))
		{
			OnEvent(&event);
		}

		OnLoop();
		OnRender();
	}

	OnCleanup();

	return 0;
}



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

	return App.OnExecute();
}
- And I have a source file for the functions OnInit(), OnRender(), OnLoop(), OnCleanup(), OnEvent().

Until here the engine is set up nicely and works fine ... then I tried to render a background image, as my interface, then I found to have the following issues: the compiler cant handle the same variables and functions, declared in the header, in different source files as it is...
and I solved that by adding the definition 'static' to the variable and functions, which i read about and seems to be because "a static variable/function doesnt cease to exit when it reaches the end of the source file" .. so it can be manipulated in the other sources!

Ok, but now, I cant render the image, and I dont know if it is because of this 'static' thing .... this is how I was trying to do it until now:

Code: Select all

//globals.h
#ifndef _GLOBALS_H_
#define _GLOBALS_H_



const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 800;
const int SCREEN_BPP = 32;


//static SDL_Surface *Screen;
static SDL_Surface *scene;
static SDL_Surface *interfaceBG;




#endif

Code: Select all

//functions.h

#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_


static void LoadIMG(char *filename, SDL_Surface *surface);
static void DrawIMG();



static void LoadIMG(char *filename, SDL_Surface *surface)
{
	surface = IMG_Load(filename);
}


static void DrawIMG(int x, int y, SDL_Surface *source, SDL_Surface *destination)
{
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, NULL, destination, &offset);
}


#endif

Code: Select all

//OnInit.cpp

#include "core.h"
#include "globals.h"
#include "functions.h"
#include "loadin.h"




bool Engine::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);
	//scene = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);

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


	SDL_WM_SetCaption("The Game - Engine", NULL);
	
        //LoadFiles();
	interfaceBG = IMG_Load("assets/interface_texture.png");

	return true;
};

Code: Select all

//OnRender.cpp

#include "core.h"
#include "globals.h"
#include "functions.h"




void Engine::OnRender()
{	
	DrawIMG(0, 0, interfaceBG, Screen);
        //if I try drawing the image loaded to the static surface 'scene'  and flip it... the program will crash
	SDL_Flip(Screen);
}

Like this it will compile with no errors and no warnings, but will only render a black screen without the image
If I try rendering the stuff to the scene layer (fine), and try flipping it, the program will crash...

P.S: SDL_FillRect() worked fine... so it is rendering to the Screen surface
I have checked the name of the file thousands of times... and it is typed correctly in the program
The DrawIMG() is set up exactly like in my other projects which works fine
Dont know in what to think anymore xD

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 11:23 am
by Trask
This may sound dumb, but anytime I try to render an image file and it just shows up blank, its usually because the either the picture is not in the directory that its looking for or something was typed wrong... the only thing I can think of without diving too deep into this is this:

Code: Select all

interfaceBG = IMG_Load("assets/interface_texture.png");
Should that be:

Code: Select all

interfaceBG = IMG_Load("assets\interface_texture.png");
With the '/' going to '\'?

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 12:15 pm
by KuramaYoko10
KuramaYoko10 wrote:I have checked the name of the file thousands of times... and it is typed correctly in the program
Looks like I had to check 1001 times, and you are right it should be "\" ... however it doesnt fix my problem :( !!

I will keep looking ...

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 1:32 pm
by Trask
Hm, well let's take that a step further. What is your folder structure?

To include a header file related to my engine I type:

Code: Select all

#include "..\engine\Advanced2D.h"
This is for a project that sits on D:\Projects\Games\project_name(here sits the .cpp file)

My engine's path is: D:\Projects\Games\engine

So that code takes me from my project's location(D:\Projects\Games\project_name), drops back a directory(D:\Projects\Games), and there it finds my 'engine' folder(D:\Projects\Games\engine(and thus the header file)).

Make sure that you didn't create a subdirectory in your project(i.e. D:\project_name\project_name(here sits the .cpp file)) which Visual Studio loves to do by default. Hopefully that makes sense, it sounds like it can't find the directory.

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 4:02 pm
by Ginto8
If it's a function that takes either a string or char*, any \ inside the quotes must be a \\. \ signifies that the character after it does not perform it's usual function. So \\ in a string or char*, or even just a char, will output '\'. ;)

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 4:14 pm
by programmerinprogress
the term for the '\' is the escape character.

you can use it just like '\n' for a newline, and a useful one is \" to use double quotes within a char*

C/C++ is a clever language isn't it :D

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 4:16 pm
by Trask
Didn't even think of that one, good catch guys.

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 5:06 pm
by KuramaYoko10
That is weird, because I went to check my other project code and look what I have (working fine):

Code: Select all

tileSheet = IMG_Load("Assets/tile_sheet02.png");
	ninja = IMG_Load("Assets/ninja_sprite.png");
	square = IMG_Load("Assets/square.png");
I have the image named "interface_texture.png" inside the folder called "assets" inside the actual game project ... that shouldnt be an error!!
It might be something else, unfortunately :(

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 5:09 pm
by KuramaYoko10
Now, can someone explain to me...

why does the program crash when I try flipping a 'static surface'??
If I put all that engine code in the same source file, and take out all the 'static' things, the rendering will be done fine... why that?


EDIT: It seems that the problem is not the 'static', because it rendered fine as static in another trial of mine... but how it is implemented in my engine =/

EDIT2: When i tried doing the rendering on my OnInit() right after the IMG_Load ... the program worked and rendered fine.. even with SDL_Flip(static Surface*)
And when I tried rendering the stuff on the OnLoop() .. it gave me the same problem as in OnRender() ... I am getting close to solution!!

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 5:43 pm
by KuramaYoko10
Ok... because of so many edits, here it goes the solution...

The surfaces, both "source" and "destination" must be a member of the struct Engine ... so now that I declared them there, I can load and render and flip any way I want and the program will work and not crash!

Sorry for making so much trouble out of it... we still need to learn to look to the stuff right under our noses first (more than one time) before looking forward!!

Re: Problems with my engine!!

Posted: Tue Feb 10, 2009 6:39 pm
by eatcomics
I was having a problem like this in blitz the other day, I was using paint.net to make a tilesheet and when I saved it I saved it as .png... I am using blitz basic so Png isn't supported, I renamed it to .bmp and it didn't work still... I ended up having to resave the file as a .bmp... so you might want to check if your image type is supported :mrgreen:

Edit: lol, I didn't read everything! :oops: Glad you found the problem though, those little errors delay progress and drive you crazy! :evil: :lol: