When I build my program, there are no errors, but when I run it, all I
see is a window with a black background, except that the title bar
caption displays correctly. I can’t figure out why. First, here’s
the build output:
michael at camille ourrpg $ make
g++ -W -Wall sdl-config --cflags
-c battle.cpp
battle.cpp: In member function ‘SDL_Surface* Battle::drawString(int,
int, std::string)’:
battle.cpp:84: warning: missing initializer for member
’SDL_Color::unused’
g++ -W -Wall sdl-config --cflags
-c character.cpp
g++ -W -Wall sdl-config --cflags
-c ally.cpp
g++ -W -Wall sdl-config --cflags
-c main.cpp
g++ -o main battle.o character.o ally.o main.o sdl-config --libs
-lSDL_image -lSDL_gfx -lSDL_ttf -lz
michael at camille ourrpg $ cat Makefile
CXX=g++
CFLAGS=-W -Wall sdl-config --cflags
#LIBS=sdl-config --libs
-lSDL_image -lSDL_gfx -lSDL_ttf -lz
-lmysqlclient_r -lmysqlpp
LIBS=sdl-config --libs
-lSDL_image -lSDL_gfx -lSDL_ttf -lz
OBJS = battle.o character.o ally.o main.o
INCS = battle.h character.h ally.h
#INCOPTS=-I/usr/local/include/mysql++/ -I/usr/include/mysql
-L/usr/local/lib
all: main
main: $(OBJS)
$(CXX) -o $@ $(OBJS) $(LIBS) $(INCOPTS)
%.o : %.cpp $(INCS)
$(CXX) $(CFLAGS) $(INCOPTS) -c $<
clean:
-rm *.o ~ core battle
michael at camille ourrpg $ cat character.h
/character.h - Header file for the character struct and function
prototypes/
#ifndef CHARACTER_H
#define CHARACTER_H
#include <SDL/SDL_image.h>
#include
using namespace std;
class Character
{
public:
Character() {}
virtual ~Character();
Character(string, long, long);
void printStats();
void setName(string);
void setCurrentHP(long);
void setMaxHP(long);
void setCurrentMP(long);
void setMaxMP(long);
string getName() { return name; }
long getCurrentHP() { return currentHP; }
long getMaxHP() { return maxHP; }
long getCurrentMP() { return currentMP; }
long getMaxMP() { return maxMP; }
long getStrength() { return STR; }
long getDexterity() { return DEX; }
long getIntelligence() { return INT; }
long getSpirit() { return SPIRIT; }
long getLuck() { return LUCK; }
void setStrength(long s) { STR = s; }
void setDexterity(long d) { DEX = d; }
void setIntelligence(long i) { INT = i; }
void setSpirit(long s) { SPIRIT = s; }
void setLuck(long l) { LUCK = l; }
void setX(int i) { xPos = i; }
int getX() { return xPos;}
void setY(int i) { yPos = i; }
int getY() { return yPos; }
string getFileName() { return filename; }
void setFileName(string f) { filename = f; }
virtual void setCurrentSprite(int) {}
bool loadImage();
SDL_Surface* getImage() { return image; }
SDL_Rect getRect() { return currentrect; }
bool initialize();
bool destroy();
private:
string name;
unsigned long currentHP, maxHP, currentMP, maxMP, STR, DEX, INT,
SPIRIT, LUCK;
int xPos, yPos;
protected:
string filename;
SDL_Rect currentrect;
SDL_Surface* image;
};
#endif
michael at camille ourrpg $ nl character.h
1 /character.h - Header file for the character struct and function
prototypes/
2 #ifndef CHARACTER_H
3 #define CHARACTER_H
4 #include <SDL/SDL_image.h>
5 #include <string>
6 using namespace std;
7 class Character
8 {
9 public:
10 Character() {}
11 virtual ~Character();
12 Character(string, long, long);
13 void printStats();
14 void setName(string);
15 void setCurrentHP(long);
16 void setMaxHP(long);
17 void setCurrentMP(long);
18 void setMaxMP(long);
19 string getName() { return name; }
20 long getCurrentHP() { return currentHP; }
21 long getMaxHP() { return maxHP; }
22 long getCurrentMP() { return currentMP; }
23 long getMaxMP() { return maxMP; }
24 long getStrength() { return STR; }
25 long getDexterity() { return DEX; }
26 long getIntelligence() { return INT; }
27 long getSpirit() { return SPIRIT; }
28 long getLuck() { return LUCK; }
29 void setStrength(long s) { STR = s; }
30 void setDexterity(long d) { DEX = d; }
31 void setIntelligence(long i) { INT = i; }
32 void setSpirit(long s) { SPIRIT = s; }
33 void setLuck(long l) { LUCK = l; }
34 void setX(int i) { xPos = i; }
35 int getX() { return xPos;}
36 void setY(int i) { yPos = i; }
37 int getY() { return yPos; }
38 string getFileName() { return filename; }
39 void setFileName(string f) { filename = f; }
40 virtual void setCurrentSprite(int) {}
41 bool loadImage();
42 SDL_Surface* getImage() { return image; }
43 SDL_Rect getRect() { return currentrect; }
44 bool initialize();
45 bool destroy();
46
47
48 private:
49 string name;
50 unsigned long currentHP, maxHP, currentMP, maxMP, STR, DEX,
INT, SPIRIT, LUCK;
51 int xPos, yPos;
52 protected:
53 string filename;
54 SDL_Rect currentrect;
55 SDL_Surface* image;
56
57 };
58 #endif
michael at camille ourrpg $ nl character.cpp
1 /character.c - Description of a basic character/
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "character.h"
5 #include <string.h>
6 #include <SDL/SDL_image.h>
7 void Character::printStats()
8 {
9 printf("This character's name is %s.\n", getName().c_str());
10 printf("%s's max HP is %ld.\n", getName().c_str(),
getMaxHP());
11 printf("%s’s max MP is %ld.\n\n", getName().c_str(),
getMaxMP());
12 }
13 Character::Character(string myName, long myMaxHP, long myMaxMP)
14 {
15 //strcpy(name,myName);
16 name = myName;
17 maxHP = myMaxHP;
18 maxMP = myMaxMP;
19 currentHP = maxHP;
20 currentMP = maxMP;
21 }
22 Character::~Character() {}
23 bool Character::initialize()
24 { return true; }
25 bool Character::destroy()
26 {
27 //SDL_FreeSurface(image);
28 return true;
29 }
30 bool Character::loadImage()
31 {
32
33 image = IMG_Load(filename.c_str());
34 if (image == NULL)
35 {
36 printf("Unable to load image %s: %s\n", filename.c_str(),
SDL_GetError());
37 return false;
38 }
39 return true;
40 }
michael at camille ourrpg $ nl ally.h
1 #ifndef ALLY_H
2 #define ALLY_H
3 #include "character.h"
4 #include <string>
5 using namespace std;
6 class Ally : public Character
7 {
8 public:
9 Ally(){}
10 Ally(string, long, long);
11 ~Ally();
12 bool initialize();
13 bool destroy();
14 void setCurrentSprite(int);
15 private:
16 enum STATE { FIGHT,WALK, RAISE, STRIKE, MAGIC, HURT, DEAD};
17 bool lHanded;
18 };
19 #endif
michael at camille ourrpg $ nl ally.cpp
1 #include "ally.h"
2 #include <stdio.h>
3 Ally::Ally(string myname, long h, long m) : Character(myname, h,
m)
4 {
5 filename = “OnionKnight1.gif”;
6 }
7 Ally::~Ally()
8 {
9 }
10 bool Ally::initialize()
11 {
12 setCurrentSprite(FIGHT);
13 // if (!loadImage()) { printf("Problem loading image: %s.\n",
filename.c_str()); }
14 return true;
15 }
16 bool Ally::destroy()
17 {return true;}
18 void Ally::setCurrentSprite(int index)
19 {
20 currentrect.w = 36;
21 currentrect.h = 50;
22 currentrect.y = 0;
23 switch (index)
24 {
25 case RAISE:
26 if (!lHanded) { index += 2; }
27 break;
28 case STRIKE:
29 if (!lHanded) { index += 2; }
30 }
31 currentrect.x = index*currentrect.w;
32
33 }
michael at camille ourrpg $ nl battle.h
1 #ifndef BATTLE_H
2 #define BATTLE_H
3 #include <SDL/SDL.h>
4 #include "SDL/SDL_ttf.h"
5 #include <string>
6 #include "character.h"
7 #include "ally.h"
8 using namespace std;
9 class Battle
10 {
11 public:
12 Battle();
13 ~Battle();
14 Ally* getParty() { return party; }
15 Ally* getParty(int i) { return &party[i]; }
16 void loadImage(int index, string filename);
17 void drawStats();
18 void drawLines();
19 void drawScreen();
20 SDL_Surface* drawString(int x, int y, string dsMessage);
21 SDL_Surface* getScreen() { return screen; }
22 SDL_Surface* getImage(int i) { return image[i]; }
23 SDL_Surface** getStatus() { return status; }
24 Uint32 getColorKey() { return colorkey; }
25 bool initialize();
26 bool destroy();
27 private:
28 Ally party[4];
29 SDL_Surface *screen, *image[4], *status[4];
30 SDL_Rect src[4], dest[4];
31 Uint32 colorkey;
32 TTF_Font *font;
33 };
34 #endif
michael at camille ourrpg $ nl battle.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL/SDL.h>
4 #include <SDL/SDL_image.h>
5 #include <SDL/SDL_gfxPrimitives.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "battle.h"
9 #include “character.h”
10 int i;
11
12 Battle::Battle()
13 {
14 //TEMP This block is temporary - don't forget to remove it
later
15 party[0] = Ally(“Michael”, 10, 7);
16 party[1] = Ally(“Amy”, 8, 13);
17 party[2] = Ally(“Feli”, 12, 15);
18 party[3] = Ally(“Jelli”, 9, 4);
19 }
20 Battle::~Battle()
21 {
22 }
23 bool Battle::initialize()
24 {
25 if (SDL_Init(SDL_INIT_VIDEO) != 0)
26 {
27 printf("Unable to initialize SDL: %s\n", SDL_GetError());
28 return false;
29 }
30 atexit(SDL_Quit);
31
32 if( TTF_Init() == -1 )
33 {
34 printf("Could not intialize the font: %s\n",
SDL_GetError());
35 return false;
36 }
37 font = TTF_OpenFont( “lazy.ttf”, 18 );
38 if( font == NULL )
39 {
40 printf("Error loading font: %s\n", SDL_GetError());
41 return false;
42 }
43 SDL_WM_SetCaption( "OurRPG - Battle Screen", NULL );
44 screen = SDL_SetVideoMode(1000, 675, 16, SDL_DOUBLEBUF |
SDL_HWSURFACE);
45 if (screen == NULL)
46 {
47 printf(“Unable to set video mode: %s\n”, SDL_GetError());
48 return false;
49 }
50 for (int i = 0; i < 4; i++)
51 {
52 status[i] = NULL;
53 party[i].setX(600);
54 party[i].setY(i * 100);
55 }
56 return true;
57 }
58 bool Battle::destroy()
59 {
60 if (screen != NULL) SDL_FreeSurface(screen);
61 if (font != NULL) TTF_CloseFont(font);
62 for (int i = 0; i < 4; i++)
63 {
64 /*SDL_FreeSurface(image[i]);
65 SDL_FreeSurface(status[i]);*/
66 }
67 return true;
68 }
69 SDL_Surface* Battle::drawString(int x, int y, string dsMessage)
70 {
71 SDL_Surface *message;
72 SDL_Color textColor = { 255, 255, 255 };
73 message = TTF_RenderText_Solid( font, dsMessage.c_str(),
textColor );
74
75 if( message == NULL )
76 {
77 printf(“Error rendering text: %s\n”, SDL_GetError());
78 return NULL;
79 }
80 SDL_Rect dest1, src1;
81 src1.x = 0;
82 src1.y = 0;
83 src1.w = message->w;
84 src1.h = message->h;
85 dest1.x = x;
86 dest1.y = y;
87 dest1.w = message->w;
88 dest1.h = message->h;
89 SDL_BlitSurface(message, &src1, screen, &dest1);
90 return message;
91 }
92 void Battle::drawLines()
93 {
94 /*Draw the battle screen lines*/
95 i = hlineColor(screen, 0, 1000, 0, 0xFFFFFFFF);
96 i = vlineColor(screen, 0, 0, 400, 0xFFFFFFFF);
97 i = hlineColor(screen, 0, 1000, 400, 0xFFFFFFFF);
98 i = vlineColor(screen, 700, 0, 400, 0xFFFFFFFF);
99 i = hlineColor(screen, 700, 1000, 0, 0xFFFFFFFF);
100 i = hlineColor(screen, 700, 1000, 100, 0xFFFFFFFF);
101 i = hlineColor(screen, 700, 1000, 200, 0xFFFFFFFF);
102 i = hlineColor(screen, 700, 1000, 300, 0xFFFFFFFF);
103 i = vlineColor(screen, 1000, 0, 400, 0xFFFFFFFF);
104 SDL_UpdateRect(screen, 0, 0, 0, 0);
105 }
106 void Battle::drawStats()
107 {
108 char tmp[255];
109 for (i = 0; i < 4; i++)
110 {
111 status[i] = drawString((party[i].getX() + 110),
(party[i].getY() + 10), party[i].getName());
112
113 sprintf(tmp, “HP: %ld/%ld”, party[i].getCurrentHP(),
party[i].getMaxHP());
114 status[i] = drawString((party[i].getX() + 110),
(party[i].getY() + 50), tmp);
115 sprintf(tmp, “MP: %ld/%ld”, party[i].getCurrentMP(),
party[i].getMaxMP());
116 status[i] = drawString((party[i].getX() + 110),
(party[i].getY() + 70), tmp);
117 }
118 }
119 void Battle::loadImage(int index, string filename)
120 {
121 image[index] = IMG_Load(filename.c_str());
122 if (image[index] == NULL)
123 {
124 printf(“Unable to load image %s: %s\n”, filename.c_str(),
SDL_GetError());
125 }
126 colorkey = SDL_MapRGB(image[index]->format, 0, 255, 0);
127 SDL_SetColorKey(image[index], SDL_SRCCOLORKEY, colorkey);
128 src[index].x = 0;
129 src[index].y = 0;
130 src[index].w = image[index]->w;
131 src[index].h = image[index]->h;
132 dest[index].x = 600;
133 dest[index].y = index * 100;
134 dest[index].w = image[index]->w;
135 dest[index].h = image[index]->h;
136 }
137 void Battle::drawScreen()
138 {
139 //Draw allies
140 for (i = 0; i < 4; i++)
141 {
142 SDL_BlitSurface(image[i], &src[i], screen, &dest[i]);
143 }
144 //Draw enemies
145
146 drawStats();
147 /Draw the battle screen lines/
148 drawLines();
149 SDL_Flip(screen);
150 }
michael at camille ourrpg $ nl main.cpp
1 #include “battle.h”
2 int main()
3 {
4 Battle mybattle;
5
6 if (!mybattle.initialize()) exit(1);
7
8 // i = 0;
9 /*Draw the allies*/
10 /* mybattle.loadImage(i++, "ffight.gif");
11 mybattle.loadImage(i++, "tfight.gif");
12 mybattle.loadImage(i++, "wmfight.gif");
13 mybattle.loadImage(i, "bmfight.gif");
14 mybattle.drawScreen();*/
15 SDL_Delay(5000);
16 mybattle.destroy();
17 return 0;
18 }
Is any of this not what it should be to the point that it wouldn’t
display anything?