OK, I’ll post all the code and the error.
main.h
Code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <wchar.h>
#include <time.h>
#include
#include
#include
#include
#include
#include “SDL.h”
#include “SDL_image.h”
#include “SDL_ttf.h”
#include “SDL_mixer.h”
using namespace std;
Tower.h
Code:
class Tower
{
public:
SDL_Surface* Image;
int x,y;
Tower(SDL_Surface* Image, int x, int y);
void Draw(SDL_Surface* Screen) const;
};
Tower.cpp
Code:
#include “main.h”
#include “tower.h”
Tower::Tower(SDL_Surface* Image, int x, int y)
: Image(Image),x(x),y(y);
{}
void Tower::Draw(SDL_Surface* Screen) const
{
SDL_Rect dest = {x,y};
SDL_BlitSurface(Image,NULL,Screen,&dest);
}
main.cpp
Code:
#include “main.h”
SDL_Surface* Screen;
SDL_Event event;
TTF_Font* Font;
int ThisTime = 0;
int LastTime = 0;
float DeltaTime = 0.0f;
const int WIDTH = 800;
const int HEIGHT = 600;
const int DEPTH = 32;
const char* TITLE = “Tower Defense”;
SDL_Color textColor = {255,255,0};
int TowerHealth = 100;
int Points = 0;
int Money = 1000;
int EnemyHealth = 50;
int Bullet_Damage = 15;
int TowerCost = 200;
SDLKey keyPressed;
SDLKey keyReleased;
bool keysHeld[323] = {false};
bool GameRunning = true;
bool GameOver = false;
SDL_Surface* Backdrop;
SDL_Rect BackdropRect;
SDL_Surface* FPS;
SDL_Rect FPSRect;
char fps[10];
SDL_Surface* POINT;
SDL_Rect POINTRect;
char point[10];
int Damage_Frames;
int Current_Frame;
const int Total_Frames = 4;
SDL_Surface* HEALTH;
SDL_Rect HealthRect;
char health[10];
SDL_Surface* MONEY;
SDL_Rect MoneyRect;
char money[10];
int mx = 0;
int my = 0;
SDL_Surface* Bullet;
SDL_Rect BulletRect;
SDL_Surface* Enemy;
SDL_Rect EnemyRect;
bool GameScreen = true;
int main(int argc, char* argv[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 1;
}
Screen = SDL_SetVideoMode(WIDTH,HEIGHT,DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption(TITLE,NULL);
if(TTF_Init() == -1)
{
return 1;
}
if(Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096) == -1)
{
return 1;
}
Backdrop = IMG_Load("Backdrop.png");
BackdropRect.x = 0;
BackdropRect.y = 0;
BackdropRect.w = 800;
BackdropRect.h = 600;
//Bullet = IMG_Load("Bullet.png");
//SDL_SetColorKey(Bullet,SDL_SRCCOLORKEY,SDL_MapRGB(Bullet->format,255,255,255));
//Enemy = IMG_Load("Enemy.png");
//SDL_SetColorKey(Enemy,SDL_SRCCOLORKEY,SDL_MapRGB(Enemy->format,255,255,255));
Font = TTF_OpenFont("Arial.ttf",20);
FPSRect.x = 1;
FPSRect.y = 1;
FPSRect.w = 0;
FPSRect.h = 0;
POINTRect.x = 1;
POINTRect.y = 20;
POINTRect.w = 0;
POINTRect.h = 0;
HealthRect.x = 1;
HealthRect.y = 40;
HealthRect.w = 0;
HealthRect.h = 0;
MoneyRect.x = 1;
MoneyRect.y = 60;
MoneyRect.w = 0;
MoneyRect.h = 0;
while(GameRunning)
{
ThisTime = SDL_GetTicks();
DeltaTime = (float)(ThisTime - LastTime) / 1000;
LastTime = ThisTime;
sprintf(fps,"FPS: %.4f", DeltaTime);
FPS = TTF_RenderText_Solid(Font,fps,textColor);
sprintf(point,"Points: %.2d", Points);
POINT = TTF_RenderText_Solid(Font,point,textColor);
sprintf(health,"Health: %.3d",TowerHealth);
HEALTH = TTF_RenderText_Solid(Font,health,textColor);
sprintf(money,"Money: %.4d",Money);
MONEY = TTF_RenderText_Solid(Font,money,textColor);
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
GameRunning = false;
}
if(event.type == SDL_KEYDOWN)
{
keyPressed = event.key.keysym.sym;
keysHeld[event.key.keysym.sym] = true;
if(keyPressed == SDLK_ESCAPE)
{
GameRunning = false;
}
}
if(event.type == SDL_KEYUP)
{
keyReleased = event.key.keysym.sym;
keysHeld[event.key.keysym.sym] = false;
}
if(event.type == SDL_MOUSEMOTION)
{
mx = event.motion.x;
my = event.motion.y;
}
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
mx = event.button.x;
my = event.button.y;
}
}
}
SDL_FillRect(Screen,NULL,SDL_MapRGB(Screen->format,0,0,0));
SDL_BlitSurface(Backdrop,NULL,Screen,&BackdropRect);
SDL_BlitSurface(FPS,NULL,Screen,&FPSRect);
SDL_BlitSurface(POINT,NULL,Screen,&POINTRect);
SDL_BlitSurface(HEALTH,NULL,Screen,&HealthRect);
SDL_BlitSurface(MONEY,NULL,Screen,&MoneyRect);
if(SDL_Flip(Screen) == -1)
{
return 1;
}
}
SDL_FreeSurface(MONEY);
SDL_FreeSurface(HEALTH);
SDL_FreeSurface(FPS);
SDL_FreeSurface(Backdrop);
SDL_FreeSurface(POINT);
TTF_CloseFont(Font);
TTF_Quit();
Mix_CloseAudio();
SDL_Quit();
return 0;
}
Errors
Code:
C:\Users\Documents\TowerDefense\tower.cpp In constructor Tower::Tower(SDL_Surface*, int, int)': 5 C:\Users\Documents\TowerDefense\tower.cpp expected
{’ before ‘;’ token
At global scope:
6 C:\Users\Documents\TowerDefense\tower.cpp expected unqualified-id before ‘{’ token
C:\Users\Documents\TowerDefense\Makefile.win [Build Error] [Objects/MingW/tower.o] Error 1
[/code]