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

15.08.2008, 15:10

[Erledigt] Problem bei Sprite Sheets

So ich habe mir den Code von Lazy Foo Productions zu Sprite Sheets geholt.
http://lazyfoo.net/SDL_tutorials/lesson06/index.php

Doch wenn ich das so schreibe dann zeigt es bei mir nicht das gewünschte ergebnis, sondern einfach nur den weißen screen.

Quellcode

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
#include "sdl.h"
#include "sdl_image.h"
#include <string>

// The surfaces 
SDL_Surface *dots = NULL;
SDL_Surface *screen = NULL;

// The window attributs

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// the event structur
SDL_Event event;

// The portions of the Sprite map to be blitted
SDL_Rect clip [4];

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect *clip);
SDL_Surface *load_image( std::string filename );
bool init ();
bool load_files ();
void clean_up ();

int main (int argc, char *args[])
{
    clip[0].x = 0;
    clip[0].y = 0;
    clip[0].w = 100;
    clip[0].h = 100;

    clip[1].x = 100;
    clip[1].y = 0;
    clip[1].w = 100;
    clip[1].h = 100;

    clip[2].x = 0;
    clip[2].y = 100;
    clip[2].w = 100;
    clip[2].h = 100;

    clip[3].x = 100;
    clip[3].y = 100;
    clip[3].w = 100;
    clip[3].h = 100;
    
    // Initialize
    if (init () == false)
    {
        return 1;
    };

    // Load files 
    if (load_files == false)
    {
        return 1;
    };

    // Fill the screen white
    SDL_FillRect (screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF));

    // Apply the images to the screen
    apply_surface(0, 0, dots, screen, &clip[0]);
    apply_surface(540, 0, dots, screen, &clip[1]);
    apply_surface(0, 380, dots, screen, &clip[2]);
    apply_surface(540, 380, dots, screen, &clip[3]);

    // Update SDL
    if (SDL_Flip (screen) == -1)
    {
        return 1;
    };

    SDL_Delay (5000);

    clean_up ();

    return 0;
};

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect *clip) 
{ 
    //Make a temporary rectangle to hold the offsets 
    SDL_Rect offset; 
    
    //Give the offsets to the rectangle 
    offset.x = x; 
    offset.y = y;

    //Blit the surface 
    SDL_BlitSurface( source, clip, destination, &offset ); 
};



SDL_Surface *load_image( std::string filename ) 
{   
    //The image that's loaded 
    SDL_Surface* loadedImage = NULL; 
    
    //The optimized image that will be used 
    SDL_Surface* optimizedImage = NULL; 
    
    //Load the image using SDL_image 
    loadedImage = IMG_Load( filename.c_str() ); 
    
    //If the image loaded 
    
    if( loadedImage != NULL ) 
    { 
        //Create an optimized image 
        optimizedImage = SDL_DisplayFormat( loadedImage ); 
        //Free the old image 
        SDL_FreeSurface( loadedImage ); 
    } 
    
    //Return the optimized image 
    return optimizedImage; 
};

bool init ()
{
    // Initialize all SDL subsystems
    if (SDL_Init (SDL_INIT_EVERYTHING) == -1)
    {
        return false;
    };

    // Set up Screen 
    screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_DOUBLEBUF);

    // If there was an error in setting up the screen
    if (screen == NULL)
    {
        return false;
    };

    // Set the windows caption
    SDL_WM_SetCaption("Sprite_Sheets test", NULL);

    // If everything fine
    return true;
};

bool load_files ()
{
    // Load the image
    dots = load_image("Data/a.bmp");
    
    // If there was an error on loading the image 
    if (dots == NULL)
    {
        return 1;
    };

    // If everything loaded fine
    return true;
};

void clean_up ()
{
    // Free the image
    SDL_FreeSurface (dots);

    // SDL Quit
    SDL_Quit ();

};

2

16.08.2008, 15:19

hat jmd ne idee was hier falsch sein könnte??

3

16.08.2008, 15:58

Und was ist, wenn du das

C-/C++-Quelltext

1
2
// Fill the screen white

   SDL_FillRect (screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF));

weck lässt?

4

16.08.2008, 16:37

dann hab ich stat nem weißen screen nen schwarzen. Habs aber eben trotzdem nochmal probiert, aber es passierte nichts anderes außer das der Bildschirm schwarz wird.

drakon

Supermoderator

Beiträge: 6 513

Wohnort: Schweiz

Beruf: Entrepreneur

  • Private Nachricht senden

5

16.08.2008, 19:41

Und das ist exakt der gleiche Code, wie von der Seite?

Vlt. Tuts ja ein kürzeres Beispiel?

6

16.08.2008, 23:59

auf der seite wird kein komplett kompilier fähiger code angegeben sondern viel mehr code stücke oder nur fkt. Von daher weiß ich auch gar nicht ob ich das so richtig gemacht habe, aber es kommen halt keine kompilier fehler und sonst läuft auch alles. Da dachte ich es geht so und sonst hab ich bis jetzt nichts brauchbares zu Sprite Sheets gefunden.

7

17.08.2008, 00:19

Re: Problem bei Sprite Sheets

Zitat von »"http://lazyfoo.net/SDL_tutorials/lesson06/index.php"«

Download the media and source code for this tutorial here.

Ganz unten.
Mein Projekt: Rise of the Sylvan

8

17.08.2008, 00:28

ohhhh....hoppla....ich sollte doch mal die seiten bis nach ganz unten lesen. :)

danke!! =)

9

17.08.2008, 01:16

also ich poste mal den Fehler und es is einfach ma wieder die konzentration oder einfach nur unvermögen meinerseits.

ich habe das geschrieben:

C-/C++-Quelltext

1
2
3
4
5
   // Load files

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


aber ich hätte das schreiben müssen:

C-/C++-Quelltext

1
2
3
4
5
   // Load files

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



also es fehlten nur dir klammern!!

10

17.08.2008, 12:20

Das ; hinter der } kannst du dir sparen.
Lieber dumm fragen, als dumm bleiben!

Werbeanzeige