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();
}
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