Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

27.06.2012, 18:27

'DisplayText': Neudefinition; Mehrfachinitialisierung

Hallo Spieleprogrammierer!

ich bin ziehmlich neu in der Spieleprogrammierung. Da ich das Game des Buches "C++ für Spieleprogrammierer" nachprogrammiert habe, möchte ich nun gerne ein Menu einbauen.
Hierfür habe ich im Netz unter http://www.aaroncox.net/tutorials/arcade/Introduction.html einen Beispielcode gefunden. Ich habe nun, um dann mein Space-Game einbinden zu können
den Code in einzelne HPP und CPP Files aufgeteilt. Alles funktioniert auch wie gewünscht, nur kann ich die Funktion für die Darstellung des Textes, namentlich "DisplayText" nicht extrahieren.
Ich probiere also folgendes:

Main.cpp:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//////////////////////////////////////////////////////////////////////////////////
// Project: SDL Introduction Tutorial
// File:    Main.cpp
//////////////////////////////////////////////////////////////////////////////////

// These three lines link in the required SDL components for our project. //
// Alternatively, we could have linked them in our project settings.    //
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#pragma comment(lib, "SDL_TTF.lib")

#include <stack>    // We'll use the STL stack to store our function pointers
#include "SDL.h"    // Main SDL header 
#include "SDL_TTF.h" // True Type Font header
#include "Defines.h" // Our defines header

#include "DisplayText.hpp"

using namespace std;   

// The STL stack can't take a function pointer as a type //
// so we encapsulate a function pointer within a struct. //
struct StateStruct 
{
    void (*StatePointer)();
};

// Global data //
stack<StateStruct> g_StateStack;    // Our state stack
SDL_Surface*    g_Bitmap = NULL;  // Our background image
SDL_Surface*    g_Window = NULL;  // Our backbuffer
SDL_Event          g_Event;         // An SDL event structure for input
int                g_Timer;         // Our timer is just an integer

// Functions to handle the three states of the game //
void Menu();
void Game();
void Exit();

// Helper functions for the main game state functions //
void DrawBackground();
void ClearScreen();

void HandleMenuInput();
void HandleGameInput();
void HandleExitInput();

// Init and Shutdown functions //
void Init();
void Shutdown();

int main(int argc, char **argv)
{
    Init();
    
    // Our game loop is just a while loop that breaks when our state stack is empty. //
    while (!g_StateStack.empty())
    {
        g_StateStack.top().StatePointer();      
    }

    Shutdown();

    return 0;
}


// This function initializes our game. //
void Init()
{
    // Initiliaze SDL video and our timer. //
    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER);
    // Setup our window's dimensions, bits-per-pixel (0 tells SDL to choose for us), //
    // and video format (SDL_ANYFORMAT leaves the decision to SDL). This function   //
    // returns a pointer to our window (back-buffer) which we assign to g_Window.   //
    g_Window = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_ANYFORMAT); 
    // Set the title of our window. //
    SDL_WM_SetCaption(WINDOW_CAPTION, 0);
    // Get the number of ticks since SDL was initialized. //
    g_Timer = SDL_GetTicks();

    // Fill our bitmap structure with information. //
    g_Bitmap = SDL_LoadBMP("data/background.bmp");     

    // We start by adding a pointer to our exit state, this way //
    // it will be the last thing the player sees of the game.   //
    StateStruct state;
    state.StatePointer = Exit;
    g_StateStack.push(state);

    // Then we add a pointer to our menu state, this will //
    // be the first thing the player sees of our game.  //
    state.StatePointer = Menu;
    g_StateStack.push(state);

    // Initialize the true type font library. //
    TTF_Init();
}

// This function shuts down our game. //
void Shutdown()
{
    // Shutdown the true type font library. //
    TTF_Quit();

    // Free our surfaces. //
    SDL_FreeSurface(g_Bitmap);
    SDL_FreeSurface(g_Window);

    // Tell SDL to shutdown and free any resources it was using. //
    SDL_Quit();
}

// This function handles the game's main menu. From here //
// the player can select to enter the game, or quit.    //
void Menu()
{
    // Here we compare the difference between the current time and the last time we //
    // handled a frame. If FRAME_RATE amount of time has, it's time for a new frame. //
    if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
    {
        HandleMenuInput();

        // Make sure nothing from the last frame is still drawn. //
        ClearScreen();

        DisplayText("Start (G)ame", 350, 250, 12, 255, 255, 255, 0, 0, 0);
        DisplayText("(Q)uit Game",  350, 270, 12, 255, 255, 255, 0, 0, 0);
            
        // Tell SDL to display our backbuffer. The four 0's will make //
        // SDL display the whole screen. //
        SDL_UpdateRect(g_Window, 0, 0, 0, 0);

        // We've processed a frame so we now need to record the time at which we did it. //
        // This way we can compare this time the next time our function gets called and  //
        // see if enough time has passed between iterations. //
        g_Timer = SDL_GetTicks();
    }   
}

// This function handles the main game. We'll control the   //
// drawing of the game as well as any necessary game logic. //
void Game()
{   
    // Here we compare the difference between the current time and the last time we //
    // handled a frame. If FRAME_RATE amount of time has, it's time for a new frame. //
    if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
    {
        HandleGameInput();

        // Make sure nothing from the last frame is still drawn. //
        ClearScreen();

        // Draw the background of our little 'game'. //
        DrawBackground();

        // Tell SDL to display our backbuffer. The four 0's will make //
        // SDL display the whole screen. //
        SDL_UpdateRect(g_Window, 0, 0, 0, 0);

        // We've processed a frame so we now need to record the time at which we did it. //
        // This way we can compare this time the next time our function gets called and  //
        // see if enough time has passed between iterations. //
        g_Timer = SDL_GetTicks();
    }   
}

// This function handles the game's exit screen. It will display //
// a message asking if the player really wants to quit.         //
void Exit()
{   
    // Here we compare the difference between the current time and the last time we //
    // handled a frame. If FRAME_RATE amount of time has, it's time for a new frame. //
    if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
    {
        HandleExitInput();

        // Make sure nothing from the last frame is still drawn. //
        ClearScreen();

        DisplayText("Quit Game (Y or N)?", 350, 250, 12, 255, 255, 255, 0, 0, 0);

        // Tell SDL to display our backbuffer. The four 0's will make //
        // SDL display the whole screen. //
        SDL_UpdateRect(g_Window, 0, 0, 0, 0);

        // We've processed a frame so we now need to record the time at which we did it. //
        // This way we can compare this time the next time our function gets called and  //
        // see if enough time has passed between iterations. //
        g_Timer = SDL_GetTicks();
    }   
}

// This function draws the background //
void DrawBackground() 
{
    // These structures tell SDL_BlitSurface() the location of what //
    // we want to blit and the destination we want it blitted to.   //
    // Presently, we blit the entire surface to the entire screen.  //
    SDL_Rect source     = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };        
    SDL_Rect destination = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
    
    // This just 'block-image transfers' our bitmap to our window. //
    SDL_BlitSurface(g_Bitmap, &source, g_Window, &destination);
}

// This function simply clears the back buffer to black. //
void ClearScreen()
{
    // This function just fills a surface with a given color. The //
    // first 0 tells SDL to fill the whole surface. The second 0  //
    // is for black. //
    SDL_FillRect(g_Window, 0, 0);
}



// This function receives player input and //
// handles it for the game's menu screen.  //
void HandleMenuInput() 
{
    // Fill our event structure with event information. //
    if ( SDL_PollEvent(&g_Event) )
    {
        // Handle user manually closing game window //
        if (g_Event.type == SDL_QUIT)
        {           
            // While state stack isn't empty, pop //
            while (!g_StateStack.empty())
            {
                g_StateStack.pop();
            }

            return;  // game is over, exit the function
        }

        // Handle keyboard input here //
        if (g_Event.type == SDL_KEYDOWN)
        {
            if (g_Event.key.keysym.sym == SDLK_ESCAPE)
            {
                g_StateStack.pop();
                return;  // this state is done, exit the function 
            }
            // Quit //
            if (g_Event.key.keysym.sym == SDLK_q)
            {
                g_StateStack.pop();
                return;  // game is over, exit the function 
            }
            // Start Game //
            if (g_Event.key.keysym.sym == SDLK_g)
            {
                StateStruct temp;
                temp.StatePointer = Game;
                g_StateStack.push(temp);
                return;  // this state is done, exit the function 
            }
        }
    }
}

// This function receives player input and //
// handles it for the main game state.  //
void HandleGameInput() 
{
    // Fill our event structure with event information. //
    if ( SDL_PollEvent(&g_Event) )
    {
        // Handle user manually closing game window //
        if (g_Event.type == SDL_QUIT)
        {           
            // While state stack isn't empty, pop //
            while (!g_StateStack.empty())
            {
                g_StateStack.pop();
            }

            return;  // game is over, exit the function
        }

        // Handle keyboard input here //
        if (g_Event.type == SDL_KEYDOWN)
        {
            if (g_Event.key.keysym.sym == SDLK_ESCAPE)
            {
                g_StateStack.pop();
                
                return;  // this state is done, exit the function 
            }           
        }
    }
}

// This function receives player input and //
// handles it for the game's exit screen.  //
void HandleExitInput() 
{
    // Fill our event structure with event information. //
    if ( SDL_PollEvent(&g_Event) )
    {
        // Handle user manually closing game window //
        if (g_Event.type == SDL_QUIT)
        {           
            // While state stack isn't empty, pop //
            while (!g_StateStack.empty())
            {
                g_StateStack.pop();
            }

            return;  // game is over, exit the function
        }

        // Handle keyboard input here //
        if (g_Event.type == SDL_KEYDOWN)
        {
            if (g_Event.key.keysym.sym == SDLK_ESCAPE)
            {
                g_StateStack.pop();
                
                return;  // this state is done, exit the function 
            }
            // Yes //
            if (g_Event.key.keysym.sym == SDLK_y)
            {
                g_StateStack.pop();
                return;  // game is over, exit the function 
            }
            // No //
            if (g_Event.key.keysym.sym == SDLK_n)
            {
                StateStruct temp;
                temp.StatePointer = Menu;
                g_StateStack.push(temp);
                return;  // this state is done, exit the function 
            }
        }
    }
}

//  Aaron Cox, 2004 //


DisplayText.hpp:

C-/C++-Quelltext

1
void DisplayText(string text, int x, int y, int size, int fR, int fG, int fB, int bR, int bG, int bB);


DisplayText.cpp:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "DisplayText.hpp"
 
// This function displays text to the screen. It takes the text //
// to be displayed, the location to display it, the size of the //
// text, and the color of the text and background.              //
void DisplayText(string text, int x, int y, int size, int fR, int fG, int fB, int bR, int bG, int bB) 
{
    // Open our font and set its size to the given parameter. //
    TTF_Font* font = TTF_OpenFont("arial.ttf", size);

    SDL_Color foreground  = { fR, fG, fB};   // Text color. //
    SDL_Color background  = { bR, bG, bB };  // Color of what's behind the text. //

    // This renders our text to a temporary surface. There //
    // are other text functions, but this one looks nice.  //
    SDL_Surface* temp = TTF_RenderText_Shaded(font, text.c_str(), foreground, background);

    // A structure storing the destination of our text. //
    SDL_Rect destination = { x, y, 0, 0 };
    
    // Blit the text surface to our window surface. //
    SDL_BlitSurface(temp, NULL, g_Window, &destination);

    // Always free memory! //
    SDL_FreeSurface(temp);

    // Close the font. //
    TTF_CloseFont(font);


und erhalte diese Fehlermeldung beim compilen:

Zitat

Fehler 11 error C2447: '{': Funktionsheader fehlt - Parameterliste im alten Stil? d:\03 coding_mapping\sdl_introduction_2\displaytext.cpp 7
Fehler 8 error C2374: 'DisplayText': Neudefinition; Mehrfachinitialisierung d:\03 coding_mapping\sdl_introduction_2\displaytext.cpp 6
Fehler 3 error C2182: 'DisplayText': Unzulässige Verwendung des Typs 'void' d:\03 coding_mapping\sdl_introduction_2\displaytext.hpp 6
Fehler 7 error C2182: 'DisplayText': Unzulässige Verwendung des Typs 'void' d:\03 coding_mapping\sdl_introduction_2\displaytext.cpp 6
Fehler 14 error C2182: 'DisplayText': Unzulässige Verwendung des Typs 'void' d:\03 coding_mapping\sdl_introduction_2\displaytext.hpp 6
Fehler 2 error C2146: Syntaxfehler: Fehlendes ')' vor Bezeichner 'text' d:\03 coding_mapping\sdl_introduction_2\displaytext.hpp 6
Fehler 6 error C2146: Syntaxfehler: Fehlendes ')' vor Bezeichner 'text' d:\03 coding_mapping\sdl_introduction_2\displaytext.cpp 6
Fehler 13 error C2146: Syntaxfehler: Fehlendes ')' vor Bezeichner 'text' d:\03 coding_mapping\sdl_introduction_2\displaytext.hpp 6
Fehler 10 error C2143: Syntaxfehler: Es fehlt ';' vor '{' d:\03 coding_mapping\sdl_introduction_2\displaytext.cpp 7
Fehler 1 error C2065: 'string': nichtdeklarierter Bezeichner d:\03 coding_mapping\sdl_introduction_2\displaytext.hpp 6
Fehler 5 error C2065: 'string': nichtdeklarierter Bezeichner d:\03 coding_mapping\sdl_introduction_2\displaytext.cpp 6
Fehler 12 error C2065: 'string': nichtdeklarierter Bezeichner d:\03 coding_mapping\sdl_introduction_2\displaytext.hpp 6
Fehler 16 error C2064: Ausdruck ergibt keine Funktion, die 10 Argumente übernimmt d:\03 coding_mapping\sdl_introduction_2\main.cpp 127
Fehler 17 error C2064: Ausdruck ergibt keine Funktion, die 10 Argumente übernimmt d:\03 coding_mapping\sdl_introduction_2\main.cpp 128
Fehler 18 error C2064: Ausdruck ergibt keine Funktion, die 10 Argumente übernimmt d:\03 coding_mapping\sdl_introduction_2\main.cpp 181
Fehler 4 error C2059: Syntaxfehler: ')' d:\03 coding_mapping\sdl_introduction_2\displaytext.hpp 6
Fehler 9 error C2059: Syntaxfehler: ')' d:\03 coding_mapping\sdl_introduction_2\displaytext.cpp 6
Fehler 15 error C2059: Syntaxfehler: ')' d:\03 coding_mapping\sdl_introduction_2\displaytext.hpp 6

Weiss jemand, wie ich diese "DisplayText"-Funktion richtig extrahiere??
Vielen Dank im Voraus für jede Hilfe!

Gruss

Sylence

Community-Fossil

Beiträge: 1 663

Beruf: Softwareentwickler

  • Private Nachricht senden

2

27.06.2012, 18:29

Du musst in der DisplayText.cpp string einbinden und entweder ein using namespace dazu packen, oder das string durch std::string ersetzen.

3

27.06.2012, 23:23

Hi Sylence!

vielen Dank für deine Antwort..stimmt, damit sind auch schon ein zwei Fehlermeldungen weg, aber ich brings einfach nicht endgültig zum laufen.
Jetzt habe ich heute tatsächlich etwa 5 Stunden damit verbracht, diesen Quelltext auseinanderzunehmen und konnte doch nix
fertiges zustande bringen!
Ich bin am Ende..also hänge ich einfach einmal das ganze Projekt als Zip an, vieleicht hat ja jemand Erbarmen..;)

Es bleiben noch diese wenigten Fehler:

Zitat

Fehler 5 error C2365: "DisplayText": Erneute Definition; vorherige Definition war "Datenvariable". c:\cplusplus\sdl_introduction\displaytext.cpp 14
Fehler 3 error C2182: 'DisplayText': Unzulässige Verwendung des Typs 'void' c:\cplusplus\sdl_introduction\displaytext.hpp 4
Fehler 2 error C2146: Syntaxfehler: Fehlendes ')' vor Bezeichner 'text' c:\cplusplus\sdl_introduction\displaytext.hpp 4
Fehler 1 error C2065: 'string': nichtdeklarierter Bezeichner c:\cplusplus\sdl_introduction\displaytext.hpp 4
Fehler 4 error C2059: Syntaxfehler: ')' c:\cplusplus\sdl_introduction\displaytext.hpp 4

Ok, ich kanns nicht anhängen, da zu gross: Nun hab ichs hier hochgeladen: http://www.zämägreimt.ch/images/SDL_Geruest.zip

Wäre HAMMER, wenn mir da jemand helfen könnte..ich denke es ist wohl eigentlich nur noch was kleines..wie immer ;)

Werbeanzeige