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
) 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 ?