-
Notifications
You must be signed in to change notification settings - Fork 0
/
Road.cpp
74 lines (66 loc) · 1.64 KB
/
Road.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
#include "Road.h"
#include <time.h>
#include <cstdlib>
Road::Road(Display* display, int width, int height) : Sprite(display, 0, 0, "road.bmp"), width(width), height(height)
{
if (height > this->surface->h) {
this->height = this->surface->h;
}
if (width > this->surface->w) {
this->width = this->surface->w;
}
}
void Road::Draw()
{
surface->w = width;
surface->h = height;
x = display->screen_width / 2 - width / 2;
SDL_Rect src_rect, dest_rect;
src_rect.x = 0;
src_rect.y = 0;
src_rect.w = width;
src_rect.h = height;
dest_rect.x = x;
dest_rect.y = y;
dest_rect.w = width;
dest_rect.h = height;
SDL_BlitSurface(surface, &src_rect, display->screen, &dest_rect);
}
void Road::ChangeSize(Road* next_road)
{
if (rand() % ROAD_CHANGE_WIDTH_RESIZING_CHANCE == 0) {
if (next_road->width > width) {
if (rand() % ROAD_CHANGE_WIDTH_RESIZING_CHANCE == 0 || next_road->width > MAX_ROAD_WIDTH) {
width = next_road->width - ROAD_WIDTH_CHANGE;
}
else {
width = next_road->width + ROAD_WIDTH_CHANGE;
}
}
else {
if (rand() % ROAD_CHANGE_WIDTH_RESIZING_CHANCE == 0 || next_road->width < MIN_ROAD_WIDTH) {
width = next_road->width + ROAD_WIDTH_CHANGE;
}
else {
width = next_road->width - ROAD_WIDTH_CHANGE;
}
}
}
else {
width = next_road->width;
}
}
void Road::Save(FILE* file)
{
fwrite(&x, sizeof(x), 1, file);
fwrite(&y, sizeof(y), 1, file);
fwrite(&width, sizeof(width), 1, file);
fwrite(&height, sizeof(height), 1, file);
}
void Road::Load(FILE* file)
{
fread(&x, sizeof(x), 1, file);
fread(&y, sizeof(y), 1, file);
fread(&width, sizeof(width), 1, file);
fread(&height, sizeof(height), 1, file);
}