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

08.04.2014, 19:04

C++/SDL arrays für mehrer bilder ... SO GEHTS

Hallo,
ich bin die tage auf eine spanischen seite geladend wo gezeit wird,
wie man mit C++/SDL Arrays benutzt und mehrer objecte/bilder gleichzeitg darstellt

genau das was ich gesucht habe !!!

Da ich mal vor einiger zeit angemacht worden bin, ich hätte keine grundkennt
nisse über Arrays, möchte ich euch mal zeigen wie das im Code gelöst worden ist ..
Der code auf der website war wohl pseudocode, weil viele sagten: code not work oder fail

jedoch nach einigen Ansetzen und sehr viel zeit
zum umändern (hat ne ganze nacht gedauert :D ) habe ich es geschafft

also ich were da allein nie im leben drauf gekommenn, dass man ein SDL_SURFACE int und Array tauglich machen muss

zudem musss man über einen bestimmten Rect (position für das object) mithilfe von Structs global machen

hier der functionierende Code von mir mit bescheibung:

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
#include <SDL.h>
#include <SDL_image.h> 
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 

// SDL.h               -> for SDL fuction
// SDL_image.h  -> for bmps,jpgs, format ect .. 
// stdio.h              -> for console, print, 
// stdlib.h             -> for console, print ..
// time.h               -> for timers and seeds the rand()

// color mask for white color makes translucent 
#define MASK 255, 255, 255
 
 // position for all background,ship,enemy objects can use with int + position
  struct position
  {
   int x;
   int y;
  };
  
 
 // Program begin //
  
int main()
{
 
 SDL_Init(SDL_INIT_EVERYTHING);
 // screen 
 SDL_Surface *screen_ ;
 SDL_Surface *image;
 SDL_Surface *ship;
 
 // now we create a multi surface with two ** so we must convert it..
 SDL_Surface **multi_pic; 
  
// position struct to multi.pos
 struct position *multipic_position;
 
  // start  
 SDL_Event event; 
 
 // struct must later covert to rect ..
 SDL_Rect position;
 
 // program loop holds if run = 1
 int run = 0;
 
 // max enemies num can create
 int multipic_num=50;
 
 // i for count multipic_num up,  j for screen buffers
 int i, j;
 
 // ship position
  int ship_position_x = 400;
  int ship_position_y = 430;
 
 // background position 
  int screen_position_x =1;
  int screen_position_y =1;
 
 // seed:random - if you reset your game all rand function goes to NULL
 srand(time(NULL));

    // Initalisire SDL
   if(SDL_Init(SDL_INIT_VIDEO) < 0 )
    {
     return -1;
    }
      
  // convert sdl surface** to sdl_surface*  -> for array  <-> to multi_pic[i]
  // convert struct position to rect for all surfaces... very amazing o.O   !!!
  // i think this is the array conatiner , isnt it ?
 
 multi_pic = (SDL_Surface**)malloc(sizeof (SDL_Surface*)*multipic_num); // SDL_Surface to Array
 multipic_position = (struct position*)malloc(sizeof (struct position)*multipic_num); // rect to int  
 
 
   // load  all pictures  in all format with  SDL_image.h
   image  = IMG_Load("background_a.jpg");
   ship  = IMG_Load("ship.jpg");
   
  // create 20 enemys and 20 x,y rand positions...
     for (i=0; i<multipic_num; i++)
     {
      multi_pic[i]  = SDL_LoadBMP ("astro.bmp"); 
      multipic_position[i].x =  rand()%590+1;
      multipic_position[i].y = 60-rand()%40+1;
     }
     
    // ...only one time !
   
    
   // we draw the screen 800, 600 , 32 bit , buffer 
   screen_ = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE);
   
//+++++++++++++++

// while is open until esc..
 
 while (!run)
 {
    // game logik..
  
  // draw background
      position.x = screen_position_x;
      position.y = screen_position_y;
      SDL_BlitSurface(image,NULL,screen_,&position); 
  
  // draw spaceship
      position.x = ship_position_x;
      position.y = ship_position_y;
      SDL_BlitSurface(ship,NULL,screen_,&position);  
   
    // NOW we draw 20 enemys and make 20 differents x,y position
     for (i=0; i<multipic_num; i++)
     {
      position.x = multipic_position[i].x;
      position.y = multipic_position[i].y;
      SDL_BlitSurface(multi_pic[i],NULL,screen_ ,&position);
      }
   
   // update/reset screen until program ends..
   SDL_Flip(screen_); 
   
   // keyboard function -> esc = quit
   while (SDL_PollEvent(&event))
   { 
    if (event.type == SDL_QUIT ||  event.key.keysym.sym == SDLK_ESCAPE)
     run = 1;
    } 
   
   
     // our enemys comes down with random speed y+ 1to 20
     for (i=0; i<multipic_num; i++)
     {
      multipic_position[i].y +=rand()%20+1; 
     }    
     
 }  // end of main loop
 
 
 // at the end ...clear the memory and quit ;-)
 
 SDL_FreeSurface(image); 
 SDL_FreeSurface(ship);
 SDL_FreeSurface(multi_pic[i]);
 
 SDL_Quit();
 return 0;  
}


für jenden anfänger mehr als nützlich !!!

hättet ihrs gewusst ?
und warum steht darüber nicht ein einiges wort im ganzen netz ?

2

08.04.2014, 19:42

Könnt ihr das in Wiki festhalten ?

Und wo ist der unterschied zwischen

-SDL_Surface **image
-SDL_Surface *image
-SDL_Surface* image

David Scherfgen

Administrator

Beiträge: 10 382

Wohnort: Hildesheim

Beruf: Wissenschaftlicher Mitarbeiter

  • Private Nachricht senden

3

08.04.2014, 19:44

Dieser Code ist gar nicht zeitgemäß und auch sehr unsauber.
Dasselbe Bild wird mehrfach geladen - unnötig!
Arrays sollte man eigentlich gar nicht mehr benutzen, für sowas gibt es std::vector.
Dann würde man auch nicht vergessen, den Speicher wieder freizugeben - wie in deinem Code geschehen.
Sorry, aber sowas solltest du dir wirklich nicht als Vorbild nehmen!

Architekt

Community-Fossil

Beiträge: 2 481

Wohnort: Hamburg

Beruf: Student

  • Private Nachricht senden

4

08.04.2014, 19:51

Im Falle das die Anzahl direkt vorliegt, wäre natürlich std::array vorzuziehen.
Der einfachste Weg eine Kopie zu entfernen ist sie zu löschen.
- Stephan Schmidt -

Cookiezzz

Frischling

Beiträge: 91

Wohnort: Deutschland

Beruf: Schüler

  • Private Nachricht senden

5

08.04.2014, 19:57

Könnt ihr das in Wiki festhalten ?

Und wo ist der unterschied zwischen

-SDL_Surface **image
-SDL_Surface *image
-SDL_Surface* image


SDL_Surface **image : Zeiger auf einen Zeiger auf ein SDL_Surface.
SDL_Surface *image : Zeiger auf ein SDL_Surface
SDL_Surface* image : Auch ein Zeiger auf ein SDL_Surface, nur in anderer Schreibweise.

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

6

08.04.2014, 21:19

130 Zeilen für eine einzelne main Methode. Uff.
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]

Werbeanzeige