casse-brick latest
Ce projet est réalisé dans le cadre du cours de Programmation Avancée en M1 Informatique de l'université de Strasbourg.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <SDL2/SDL.h>
2#include <SDL_image.h>
3#include <SDL2/SDL_timer.h>
4
5#include "WALL.h"
6#include "platform.h"
7#include "hexagon.h"
8#include "grille.h"
9#include "triangle.h"
10
11#include "CollisionManager.h"
12#include "Ball.h"
13
14#include <iostream>
15#include <memory>
16
17#define WIDTH 800
18#define HEIGHT 600
19#define DELAY 3000
20
31int main (int argc, char **argv)
32{
33
34 int status = std::system("pwd");
35
36 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
37 fprintf(stderr, "SDL failed to initialise: %s\n", SDL_GetError());
38 return -1;
39 }
40
41 /* Creates a SDL window */
42 std::shared_ptr<SDL_Window> window (SDL_CreateWindow("SDL Example", /* Title of the SDL window */
43 SDL_WINDOWPOS_UNDEFINED, /* Position x of the window */
44 SDL_WINDOWPOS_UNDEFINED, /* Position y of the window */
45 WIDTH, /* Width of the window in pixels */
46 HEIGHT, /* Height of the window in pixels */
47 0), SDL_DestroyWindow); /* Additional flag(s) */
48
49 /* Checks if window has been created; if not, exits program */
50 if (window == nullptr) {
51 fprintf(stderr, "SDL window failed to initialise: %s\n", SDL_GetError());
52 return -1;
53 }
54
55 std::shared_ptr<SDL_Renderer> renderer (SDL_CreateRenderer(window.get(),
56 -1,
57 SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
58 ), SDL_DestroyRenderer);
59
60 if (renderer == nullptr){
61 fprintf(stderr, "SDL renderer failed to initialise: %s\n", SDL_GetError());
62 return -1;
63 }
64
65 int imgFlags = IMG_INIT_PNG;
66 if (!(IMG_Init(imgFlags) & imgFlags)) {
67 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
68 return -1;
69 }
70
71
72
73 SDL_Event e;
74 bool quit = false;
75 bool choice = false;
76
77 std::cout<<"width: "<<WIDTH<<"height: "<<HEIGHT<<std::endl;
78 SDL_RenderClear(renderer.get());
79 CollisionManager collisionManager = CollisionManager();
80
81 Grille pregrille = Grille(WIDTH, HEIGHT, RECTANGLE);
82
83 WALL left = WALL( 0, 10, 10, HEIGHT,"left", false);
84 WALL right = WALL( WIDTH - 10, 10, 10, HEIGHT,"right", false);
85 WALL top = WALL( 0, 0, WIDTH, 10,"top", false);
86 WALL bottom = WALL( 0, HEIGHT-10, WIDTH, 10,"bottom", true);
87
88 PLATFORM plat = PLATFORM(renderer,((WIDTH-10)/2),(HEIGHT/4)*3-5,10,WIDTH-10,10);
89 Ball ball2 = Ball(((WIDTH-10)/2),((HEIGHT-10)/2) + 50,10,{255, 0, 0, 255},"circle");
90
91 SDL_Rect menu = {(WIDTH-10)/4,(HEIGHT-10)/4,(WIDTH-20)/4*2,(HEIGHT-20)/4*2};
92
93 // Calculate the width of each smaller rectangle
94 int rectWidth = (menu.w-40) / 3;
95 // Calculate the gap between rectangles
96 int gap = 10;
97
98 std::array<std::string, 3> imagePaths = {
99 "../assets/rect.png",
100 "../assets/triangle.png",
101 "../assets/hexa.png"
102 };
103
104 std::array<SDL_Texture*, 3> textures{};
105 for (size_t i = 0; i < textures.size(); ++i) {
106 SDL_Surface* loadedSurface = IMG_Load(imagePaths[i].c_str());
107 if (loadedSurface == NULL) {
108 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to load image %s! SDL_image Error: %s\n", imagePaths[i].c_str(), IMG_GetError());
109 return -1;
110 }
111 textures[i] = SDL_CreateTextureFromSurface(renderer.get(), loadedSurface);
112 if (textures[i] == NULL) {
113 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create texture from %s! SDL Error: %s\n", imagePaths[i].c_str(), SDL_GetError());
114 return -1;
115 }
116 SDL_FreeSurface(loadedSurface);
117 }
118
119
120 std::shared_ptr<Grille> PreGrillePtr = std::make_shared<Grille>(std::move(pregrille));
121 std::shared_ptr<Ball> BallPtr2 = std::make_shared<Ball>(std::move(ball2));
122 std::shared_ptr<PLATFORM> PlatPtr = std::make_shared<PLATFORM>(std::move(plat));
123 std::shared_ptr<WALL> lWall = std::make_shared<WALL>(std::move(left));
124 std::shared_ptr<WALL> rWall = std::make_shared<WALL>(std::move(right));
125 std::shared_ptr<WALL> tWall = std::make_shared<WALL>(std::move(top));
126 std::shared_ptr<WALL> bWall = std::make_shared<WALL>(std::move(bottom));
127
128 collisionManager.addCollider(BallPtr2);
129 collisionManager.addCollider(PlatPtr);
130 collisionManager.addCollider(lWall);
131 collisionManager.addCollider(rWall);
132 collisionManager.addCollider(tWall);
133 collisionManager.addCollider(bWall);
134
135 for (const auto& row : PreGrillePtr->grille) {
136 for (const auto& brique : row) {
137 collisionManager.addCollider(brique);
138 }
139 }
140 SDL_Color white = {255,255,255,255};
141
142 int selectedRectIndex = 0;
143
144 while (!quit){
145
146 quit = collisionManager.detectCollisions();
147
148 SDL_RenderClear(renderer.get());
149 while (SDL_PollEvent(&e)){
150 if (e.type == SDL_QUIT){
151 quit = true;
152 }
153 if (e.type == SDL_KEYDOWN){
154 if(!choice) {
155 if (e.key.keysym.sym == SDLK_LEFT || e.key.keysym.scancode == 4) selectedRectIndex = (selectedRectIndex - 1 + 3) % 3;
156 else if (e.key.keysym.sym == SDLK_RIGHT || e.key.keysym.scancode == 7) selectedRectIndex = (selectedRectIndex + 1) % 3;
157 else if (e.key.keysym.sym == SDLK_RETURN){
158 choice = true;
159 for (auto texture : textures) {
160 SDL_DestroyTexture(texture);
161 }
162 for (const auto& row : PreGrillePtr->grille) {
163 for (const auto& brique : row) {
164 collisionManager.removeCollider(brique);
165 }
166 }
167 Grille grille = Grille(WIDTH, HEIGHT, selectedRectIndex == 0 ?RECTANGLE : selectedRectIndex==1?TRIANGLE:HEXAGON);
168 std::shared_ptr<Grille> GrillePtr = std::make_shared<Grille>(std::move(grille));
169 for (const auto& row : GrillePtr->grille) {
170 for (const auto& brique : row) {
171 collisionManager.addCollider(brique);
172 }
173 }
174 }
175 }else{
176 PlatPtr->input(e.key,false);
177 }
178 }
179 if (e.type == SDL_KEYUP){
180 PlatPtr->input(e.key,true);
181 }
182 if (e.type == SDL_MOUSEMOTION && choice){
183 PlatPtr->Velocity= {0,0};
184 int x,y;
185 SDL_GetMouseState(&x,&y);
186 auto col = std::dynamic_pointer_cast<RectCollider>(PlatPtr->getCollider());
187 PlatPtr->getCollider()->p = {static_cast<float>(x) -
188 col->width/2,(HEIGHT/4)*3-5};
189 }
190 }
191
192
193 for(auto object:collisionManager.colliders){
194 object->render(renderer,white,white);
195 auto movingObject = std::dynamic_pointer_cast<MovingObject>(object);
196
197 if (movingObject && choice){
198 movingObject->move();
199 }
200 }
201 if (!choice){
202 SDL_SetRenderDrawColor(renderer.get(), 255,192,203,255);
203
204 SDL_RenderFillRect(renderer.get(),&menu);
205
206 for (size_t i = 0; i < textures.size(); ++i) {
207 SDL_Rect dstRect = {menu.x + gap + (rectWidth + gap) * static_cast<int>(i), menu.y + 10, rectWidth, menu.h / 3};
208 SDL_RenderCopy(renderer.get(), textures[i], NULL, &dstRect);
209
210 if (i == selectedRectIndex) {
211 SDL_SetRenderDrawColor(renderer.get(), 255, 0, 0, 255);
212 SDL_RenderDrawRect(renderer.get(), &dstRect);
213 }
214 }
215 SDL_SetRenderDrawColor(renderer.get(), 0,0,0,255);
216
217 }
218
219 SDL_RenderPresent(renderer.get());
220 }
221
222 SDL_DestroyRenderer(renderer.get());
223 SDL_DestroyWindow(window.get());
224
225
226
227 return 0;
228}
@ TRIANGLE
Definition brique.h:28
@ HEXAGON
Definition brique.h:27
@ RECTANGLE
Definition brique.h:26
The Ball class represents a ball object in the game.
Definition Ball.h:20
The CollisionManager class manages collision detection and resolution.
bool detectCollisions()
Detects collisions between collisionObject.
void removeCollider(const std::shared_ptr< CollidingObject > &collider)
Removes a collisionObject from the collision manager.
std::vector< std::shared_ptr< CollidingObject > > colliders
The collection of colliding objects.
void addCollider(const std::shared_ptr< CollidingObject > &collider)
Adds a collisionObject to the collision manager.
The Grille class represents the game grid for blocks/bricks.
Definition grille.h:22
The PLATFORM class represents a movable platform object.
Definition platform.h:20
Definition WALL.h:18
#define WIDTH
Definition main.cpp:17
int main(int argc, char **argv)
The main function of the game.
Definition main.cpp:31
#define HEIGHT
Definition main.cpp:18