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

  • »sr-programmierung« ist der Autor dieses Themas

Beiträge: 142

Wohnort: Sachsen

Beruf: Student

  • Private Nachricht senden

1

14.07.2012, 22:34

Bewegungsproblem in der SDL

Hallo

ich programmiere ab und zu immer mal wieder was mit der SDL und C++.

Ich habe jetzt folgendes Problem mit dem Code:

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
// the headers
#include "SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"

#include <iostream>
#include <string>
#include <fstream>

// the attributes of the screen
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

// the surfaces that will be used
SDL_Surface *screen = NULL;
SDL_Surface *gras = NULL;
SDL_Surface *hero = NULL;
SDL_Surface *hero2 = NULL;

// the event structure that will be used
SDL_Event event;

// the font that will be used
TTF_Font *font = NULL;

// the color of the font
SDL_Color textColor = {255, 255, 255};
SDL_Color background = {0, 0, 0};

// file stream for the game log
std::ofstream logger ("log.txt");

void log (std::string message)
{
    // write message to the file
    logger << message << std::endl;

    // flush the buffer
    logger.flush();
}

SDL_Surface *load_image (std::string filename)
{
    // temp storage for the surface
    SDL_Surface *loadedImage = NULL;

    //optimized image that will be used
    SDL_Surface *optimizedImage = NULL;

    // load the image
    loadedImage = IMG_Load( filename.c_str());

    // create the optimized image
    optimizedImage = SDL_DisplayFormat (loadedImage);

    // free the old image
    SDL_FreeSurface(loadedImage);

    // check for loading errors
    if (loadedImage != NULL)
    {
        // map the color key
        Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF);

        // set all pixels in the colorkey to be transparent
        SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
    }

    // return the optimized image
    return optimizedImage;
}

void apply_surface (int x, int y, SDL_Surface *source, SDL_Surface *destination)
{
    // make a temp rect for holding the offset
    SDL_Rect offset;

    // gives the offsets to the rectangle
    offset.x = x;
    offset.y = y;

    // blit the surfaces
    SDL_BlitSurface(source, NULL, destination, &offset);
}

bool init()
{
    // init the SDL
    log("Initializing...");

    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        // quit app
        return 1;
    }

    // setup the screen
    log("Set screen...");

    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    // if there was a error with the screen setup
    if (screen == NULL)
    {
        return 1;
    }

    // init SDL_ttf
    log("set font...");

    if (TTF_Init() == -1)
    {
        return false;
    }

    // set the window caption
    SDL_WM_SetCaption("SDL GAME", NULL);

    // no problem?
    return true;
}

bool load_files()
{

    // load the images
    hero = load_image("gfx/foo.bmp");
    gras = load_image("gfx/grasbig.bmp");

    // open the font
    font = TTF_OpenFont("fonts/lazy.ttf", 20);

    if (font == NULL)
    {
        return false;
    }

    if (gras == NULL)
    {
        return false;
    }

//    if (hero == NULL)
//    {
//        return false;
//    }

    return true;
}

// cleaning up
void clean_up()
{
    // free the images
//    SDL_FreeSurface(hero);
    SDL_FreeSurface(gras);
    SDL_FreeSurface(hero2);

    // close the used font
    TTF_CloseFont(font);

    // quit sdl_ttf
    TTF_Quit();

    // quit
    SDL_Quit();
}

int heroX = 0;
int heroY = 0;
//int heroSpeedX = heroSpeedY = 5;


int main(int argc, char *args[])
{
    // make sure the app waits for quit
    bool quit = false;

    log("init in main function...");

    // init
    if (init() == false)
    {
        return 1;
    }

    log("load the files...");

    if ( load_files() == false)
    {
        return 1;
    }

    log ("render text...");

    // render the text
    hero2 = TTF_RenderText_Solid(font, "@", textColor);

    if (hero2 == NULL)
    {
        return 1;
    }

    log("blitting....");

    // apply the background to the screen
//  apply_surface(0, 0, gras, screen);

//    // draw the hero as @ on the screen
    apply_surface(heroX, heroY, hero2, screen);
//
    // apply the hero to the screen
//  apply_surface(400, 300, hero, screen);

    log("update screen ...");

    // update the screen
    if (SDL_Flip(screen) == -1)
    {
        return 1;
    }

    // while the user hadnt quit
    while (quit == false)
    {
        // while there's an event to handle
        while (SDL_PollEvent(&event))
        {
            // if the user has Xed out the window
            if (event.type == SDL_QUIT)
            {
                // quit the app
                quit = true;
            }
        }

        // get the keystates
        Uint8 *keystates = SDL_GetKeyState(NULL);

        if (keystates[SDLK_UP])
        {
             if (heroY > 0)
            {
                heroY = heroY - 17;
                apply_surface(heroX, heroY, hero2, screen);
            }
            else
            {
                heroY = 0;
                apply_surface(heroX, heroY, hero2, screen);
            }
        }

        if (keystates[SDLK_DOWN])
        {
            if (heroY < SCREEN_HEIGHT)
            {
                heroY = heroY + 17;
                apply_surface(heroX, heroY, hero2, screen);
            }
            else
            {
                heroY = SCREEN_HEIGHT;
                apply_surface(heroX, heroY, hero2, screen);
            }

        }

        if (keystates[SDLK_LEFT])
        {
            heroX = heroX - 17;
            apply_surface(heroX, heroY, hero2, screen);
        }

        if (keystates[SDLK_RIGHT])
        {
            heroX = heroX + 17;
            apply_surface(heroX, heroY, hero2, screen);
        }

        log("update screen ...");

        // update the screen
        if (SDL_Flip(screen) == -1)
        {
            return 1;
        }

    }

    clean_up();

    // return
    return 0;
}


Wenn ich es ausführe, habe ich
  1. keine Kollisionserkennung mit dem oberen rand
  2. man bewegt sich zu schnell, aber das kann man bestimmt mit nem timer lösen

hat jemand eine idee?

MfG
Sr

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

2

14.07.2012, 23:21

Jup, ich würde sagen Du benutzt mal den Debugger. Den der IDE, nicht die des Forums.

Tipp zu 1:
2 ist größer als 0, aber 2-17 ist kleiner als 0.

Tipp zu 2:
17 ist eine nette Primzahl, aber das sind 7 und 11 auch.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »BlueCobold« (14.07.2012, 23:37)