graphics.cpp:19: error: `exit' undeclared (first use this function)
graphics.cpp:19: error: (Each undeclared identifier is reported only once for each function it appears in.)
graphics.cpp:22: error: `atexit' undeclared (first use this function)
Is there something wrong with my files? When I put the code back in to the main cpp file it works fine. :/
Here is what I've got so far.
globals.h
/*--------------------------------------------------------------+
| Project: Side Scroller |
| File: globals.h |
+--------------------------------------------------------------*/
#pragma once
/* Defines */
#define TITLE_STRING "Side Scroller"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define BPP 32
#define SCREEN_FLAGS SDL_SWSURFACE
/* Global Variables */
SDL_Surface *g_image;
SDL_Surface *g_screen;
SDL_Event g_event;
/*--------------------------------------------------------------+
| Project: Side Scroller |
| File: graphics.h |
+--------------------------------------------------------------*/
#ifndef GRAPHICS_H
#define GRAPHICS_H
bool graphics_init(int w, int h, bool fullscreen);
void graphics_shutdown();
void graphics_setresolution(int w, int h, bool fullscreen);
class sprite
{
public:
sprite();
~sprite();
bool init(char *dir, SDL_Surface *screen);
void draw();
void clearBG();
void updateBG();
float getX(){return m_x;};
float getY(){return m_y;};
float getW(){return m_w;};
float getH(){return m_h;};
void setX (float x);
void setY (float y);
void setW (float w);
void setH (float h);
void setPos (float x, float y);
void addX (float x);
void addY (float y);
private:
SDL_Surface *m_image;
SDL_Surface *m_back;
SDL_Surface *m_screen;
float m_x;
float m_y;
float m_oldx;
float m_oldy;
float m_w;
float m_h;
};
#endif
/*--------------------------------------------------------------+
| Project: Side Scroller |
| File: graphics.cpp |
+--------------------------------------------------------------*/
/* Includes */
#include "SDL/SDL.h"
#include "graphics.h"
#include "globals.h"
bool graphics_init(int w, int h, bool fullscreen)
{
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
if(fullscreen)
g_screen = SDL_SetVideoMode(w, h, BPP, SCREEN_FLAGS | SDL_FULLSCREEN);
else
g_screen = SDL_SetVideoMode(w, h, BPP, SCREEN_FLAGS);
if(g_screen == NULL)
{
printf("Unable to set 640 x 480 video: %s\n", SDL_GetError());
exit(1);
}
SDL_WM_SetCaption(TITLE_STRING, 0);
SDL_ShowCursor(0);
};
void graphics_shutdown()
{
SDL_FreeSurface(g_image);
SDL_FreeSurface(g_screen);
SDL_Quit();
}
void graphics_setresolution(int w, int h, bool fullscreen)
{
//
};
sprite::~sprite()
{
SDL_FreeSurface(m_image);
}
bool sprite::init(char *dir, SDL_Surface *screen)
{
SDL_Surface *temp;
if((temp = SDL_LoadBMP(dir)) == NULL) return false;
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 0, 255, 0));
m_image = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
m_screen = screen;
m_w = m_image->w;
m_h = m_image->h;
return true;
}
void sprite::draw()
{
SDL_Rect dest;
dest.x = (int)m_x; dest.y = (int)m_y;
SDL_BlitSurface(m_image, NULL, m_screen, &dest);
}
void sprite::clearBG()
{
SDL_Rect dest;
dest.x = (int)m_oldx;
dest.y = (int)m_oldy;
dest.w = (int)m_w;
dest.h = (int)m_h;
SDL_BlitSurface(m_back, NULL, m_screen, &dest);
}
void sprite::updateBG()
{
SDL_Rect srcrect;
srcrect.w = (int)m_w;
srcrect.h = (int)m_h;
srcrect.x = (int)m_x;
srcrect.y = (int)m_y;
m_oldx = m_x;
m_oldy = m_y;
SDL_BlitSurface(m_screen, &srcrect, m_back, NULL);
}
void sprite::setX (float x)
{
m_x = x;
}
void sprite::setY (float y)
{
m_y = y;
}
void sprite::setW (float w)
{
m_w = w;
}
void sprite::setH (float h)
{
m_h = h;
}
void sprite::setPos (float x, float y)
{
m_x = x;
m_y = y;
}
void sprite::addX (float x)
{
m_x += x;
}
void sprite::addY (float y)
{
m_y += y;
}
/*--------------------------------------------------------------+
| Project: Side Scroller |
| File: main.cpp |
+--------------------------------------------------------------*/
/* Includes */
#include "SDL/SDL.h"
#include <string>
#include <stdio.h>
#include "globals.h"
#include "graphics.h"
/* Namespace */
using namespace std;
/* Forward Declarations */
void init();
void game();
void shutdown();
void DrawBackground();
void ClearScreen();
/* Functions */
int main(int argc, char **argv)
{
init();
int done=0;
while(done == 0)
{
while(SDL_PollEvent(&g_event))
{
if(g_event.type == SDL_QUIT) {done = 1;}
if(g_event.type == SDL_KEYDOWN)
{
if(g_event.key.keysym.sym == SDLK_ESCAPE ) {done = 1;}
}
}
game();
}
shutdown();
return 0;
}
void init()
{
graphics_init(SCREEN_WIDTH, SCREEN_HEIGHT, false);
g_image = SDL_LoadBMP("background.bmp");
}
void shutdown()
{
graphics_shutdown();
}
void game()
{
ClearScreen();
DrawBackground();
SDL_UpdateRect(g_screen, 0, 0, 0, 0);
}
void DrawBackground()
{
SDL_Rect source = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
SDL_Rect destination = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
SDL_BlitSurface(g_image, &source, g_screen, &destination);
}
void ClearScreen()
{
SDL_FillRect(g_screen, 0, 0);
}