-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprites-rotating.cpp
executable file
·144 lines (120 loc) · 4.15 KB
/
Sprites-rotating.cpp
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
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <SDL.h>
#include <sge_rotation.h>
#include <iostream>
#include <ctime>
const int NbSprites = 2000;
////////////////////////////////////////////////////////////
// SDL displaying 1000 sprites
////////////////////////////////////////////////////////////
int SpritesRotatingSDL()
{
// Initialize the random numbers generator
srand(static_cast<unsigned int>(time(NULL)));
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
return 1;
// Create the main window
SDL_Surface* App = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (App == NULL)
return 1;
// Load five images
SDL_Surface* Images[5];
Images[0] = SDL_DisplayFormat(SDL_LoadBMP("image1.bmp"));
Images[1] = SDL_DisplayFormat(SDL_LoadBMP("image2.bmp"));
Images[2] = SDL_DisplayFormat(SDL_LoadBMP("image3.bmp"));
Images[3] = SDL_DisplayFormat(SDL_LoadBMP("image4.bmp"));
Images[4] = SDL_DisplayFormat(SDL_LoadBMP("image5.bmp"));
if (!Images[0] || !Images[1] || !Images[2] || !Images[3] || !Images[4])
{
return 1;
}
// Create a lot of sprites
struct Sprite
{
SDL_Rect Rect;
SDL_Surface* Image;
};
Sprite Sprites[NbSprites];
for (int i = 0; i < NbSprites; ++i)
{
Sprites[i].Image = Images[rand() % 5];
Sprites[i].Rect.x = rand() % 700;
Sprites[i].Rect.y = rand() % 500;
Sprites[i].Rect.w = Sprites[i].Image->w;
Sprites[i].Rect.h = Sprites[i].Image->h;
}
// Start the timer
Uint32 Time = SDL_GetTicks();
int NbFrames = 0;
// Start game loop
while (SDL_GetTicks() < Time + 5000)
{
// Clear the screen
SDL_FillRect(App, NULL, SDL_MapRGB(App->format, 0, 0, 0));
// Draw the sprites
float Angle = (SDL_GetTicks() - Time) / 10.f;
for (int i = 0; i < NbSprites; ++i)
{
Uint32 Background = SDL_MapRGB(Sprites[i].Image->format, 0, 0, 0);
SDL_Surface* Surface = sge_transform_surface(Sprites[i].Image, Background, Angle, 1.f, 1.f, SGE_TTMAP);
Sprites[i].Rect.w = Surface->w;
Sprites[i].Rect.h = Surface->h;
SDL_BlitSurface(Surface, NULL, App, &Sprites[i].Rect);
SDL_FreeSurface(Surface);
}
// Finally, display rendered frame on screen
SDL_Flip(App);
NbFrames++;
}
// Exit SDL
SDL_Quit();
return NbFrames;
}
////////////////////////////////////////////////////////////
// SFML displaying 1000 sprites
////////////////////////////////////////////////////////////
int SpritesRotatingSFML()
{
// Create the main window
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML_App");
// Load five images
sf::Texture Textures[5];
if (!Textures[0].loadFromFile("image1.bmp") || !Textures[1].loadFromFile("image2.bmp") ||
!Textures[2].loadFromFile("image3.bmp") || !Textures[3].loadFromFile("image4.bmp") ||
!Textures[4].loadFromFile("image5.bmp"))
{
return 1;
}
// Create a lot of sprites
sf::Sprite Sprites[NbSprites];
for (int i = 0; i < NbSprites; ++i)
{
Sprites[i].setTexture(Textures[rand() % 5]);
Sprites[i].setPosition(rand() % 700, rand() % 500);
Sprites[i].setOrigin(Sprites[i].getLocalBounds().width / 2.f, Sprites[i].getLocalBounds().height / 2.f);
}
// Start the timer
sf::Clock Clock;
int NbFrames = 0;
// Start game loop
while (Clock.getElapsedTime().asSeconds() < 5.f)
{
// Clear the screen
App.clear();
// Draw the sprites
float Angle = Clock.getElapsedTime().asSeconds() * 100.f;
for (int i = 0; i < NbSprites; ++i)
{
Sprites[i].setRotation(Angle);
App.draw(Sprites[i]);
}
// Finally, display rendered frame on screen
App.display();
NbFrames++;
}
return NbFrames;
}