This repository was archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphics.cpp
More file actions
131 lines (111 loc) · 3.74 KB
/
graphics.cpp
File metadata and controls
131 lines (111 loc) · 3.74 KB
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
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "defines.h"
#include "snake.hpp"
#include "objects.hpp"
#include "graphics.hpp"
MainWindow::MainWindow() {
this-> window = NULL; // Affichage fenêtre
this-> renderer = NULL; // Surface fenêtre
}
MainWindow::~MainWindow() {
SDL_DestroyRenderer(renderer); // Destruction renderer
SDL_DestroyWindow(window); // Destruction fenêtre
}
void MainWindow::init(const char *name, int width, int height) {
// Init window & randerer
if (SDL_CreateWindowAndRenderer(width, height, 0, &window, &renderer) < 0) {
printf("Erreur lors de la creation d'un renderer : %s\n", SDL_GetError());
return;
}
SDL_SetWindowTitle(window, name);
}
SDL_Renderer *MainWindow::getRenderer() {
return this-> renderer;
}
void rendererReset(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
}
void printRectOnRenderer(SDL_Rect rect, SDL_Renderer* renderer, int r, int g, int b) {
rect.x = (rect.x + 1) * TILE_SIZE;
rect.y = (rect.y + 1) * TILE_SIZE;
rect.w = TILE_SIZE;
rect.h = TILE_SIZE;
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_RenderFillRect(renderer, &rect);
}
void printImgOnRenderer(SDL_Texture* texture, SDL_Renderer* renderer, SDL_Rect pos, int angle) {
pos.x = (pos.x + 1) * TILE_SIZE;
pos.y = (pos.y + 1) * TILE_SIZE;
pos.w = TILE_SIZE;
pos.h = TILE_SIZE;
SDL_RenderCopyEx(renderer, texture, NULL, &pos, angle, NULL, SDL_FLIP_NONE);
}
SDL_Texture* loadSDLImg(const char* file, SDL_Renderer* renderer) {
SDL_Surface* img = IMG_Load(file);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
return texture;
}
Playground::Playground(SDL_Renderer* newRenderer) {
this-> renderer = newRenderer;
this-> shield = loadSDLImg("sprites/shield.png", this-> renderer);
eraseAndWalls();
}
Playground::~Playground() {
SDL_DestroyTexture(this-> shield);
}
void Playground::eraseAndWalls() {
rendererReset(this-> renderer); // Clear the window
SDL_SetRenderDrawColor(this-> renderer, 20, WALL_COLOR);
for (int i = 0; i < GRID_WIDTH+1; i++) {
for (int j = 0; j < GRID_HEIGHT+1; j++) {
if ((i+j)%2 == 0) printRectOnRenderer({i, j}, this-> renderer, FLOOR_COLOR1);
else printRectOnRenderer({i, j}, this-> renderer, FLOOR_COLOR2);
}
}
SDL_SetRenderDrawColor(this-> renderer, WALL_COLOR, 255);
for (int i = 0; i < TILE_SIZE; i++) {
SDL_Rect rect = {i, i, TILE_SIZE*(3+GRID_WIDTH)-(i-1)*2-1, TILE_SIZE*(3+GRID_HEIGHT)-(i-1)*2-1};
SDL_RenderDrawRect(this-> renderer, &rect);
}
}
void Playground::printShieldIndicator() {
printImgOnRenderer(this-> shield, this-> renderer, {GRID_WIDTH, GRID_HEIGHT+3});
}
void Playground::drawScore(int score) {
int numDigits = 0;
for (int n = score; n; n /= 10) numDigits++;
while (numDigits) {
numDigits--;
drawDigit( score % 10, TILE_SIZE + numDigits * 4 * DIGIT_PIXEL_SIZE, (GRID_HEIGHT+4)*TILE_SIZE );
score /= 10;
}
}
void Playground::drawDigit(int digit, int xp, int yp) {
SDL_Rect rect = { 0, 0, DIGIT_PIXEL_SIZE, DIGIT_PIXEL_SIZE };
/// Loop if overflow to prevent wrong memory access.
digit = digit % 10;
for ( int y = 0; y < 5; ++y )
{
for ( int x = 0; x < 3; ++x )
{
if ( (*(digits[digit]))[y * 3 + x] == 'x' )
{
// Draw shadow.
SDL_SetRenderDrawColor( this->renderer, 0, 0, 0, 255 );
rect.x = xp + x * DIGIT_PIXEL_SIZE + DIGIT_PIXEL_SIZE;
rect.y = yp + y * DIGIT_PIXEL_SIZE + DIGIT_PIXEL_SIZE;
SDL_RenderFillRect( this->renderer, &rect );
// Draw white square.
SDL_SetRenderDrawColor( this->renderer, 255, 255, 255, 255 );
rect.x -= DIGIT_PIXEL_SIZE;
rect.y -= DIGIT_PIXEL_SIZE;
SDL_RenderFillRect( this->renderer, &rect );
}
}
}
}