Problems with my engine!!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Problems with my engine!!

Post 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
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: Problems with my engine!!

Post 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 '\'?
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Re: Problems with my engine!!

Post 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 ...
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: Problems with my engine!!

Post 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.
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Problems with my engine!!

Post 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 '\'. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Problems with my engine!!

Post 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
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: Problems with my engine!!

Post by Trask »

Didn't even think of that one, good catch guys.
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Re: Problems with my engine!!

Post 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 :(
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Re: Problems with my engine!!

Post 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!!
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Re: Problems with my engine!!

Post 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!!
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Problems with my engine!!

Post 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:
Image
Post Reply