/
CFruit.cpp
100 lines (70 loc) · 2.74 KB
/
CFruit.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
#include "CFruit.h"
//---------------------------------------------------------------------------
CFruit::CFruit(SDL_Window *MainWindow, SDL_Renderer *Renderer){
this->MainWindow = MainWindow;
this->Renderer = Renderer;
PreviousFruit = CTile(0,0,0,4);
CurrentFruit = CTile(0,0,0,4);
Eaten = true;
currentTexture = 0;
srand (time(NULL));
}
//---------------------------------------------------------------------------
CFruit::~CFruit(){
for(SDL_Texture *tex : FruitTextures){
SDL_DestroyTexture(tex);
}
}
//---------------------------------------------------------------------------
bool CFruit::OnInit(){
SDL_Surface *tempSurface = NULL;
SDL_Texture *tempTexture = NULL;
//----------------------------------------------------------------
if( (tempSurface = SDL_LoadBMP("bitmaps/blueFruit.bmp")) == NULL ){
CMessage::showformat("CFruit::OnInit - bitmapa nenactena {}",{"blueFruit.bmp"});
return false;
}
tempTexture = SDL_CreateTextureFromSurface(Renderer,tempSurface);
FruitTextures.push_back(tempTexture);
if( (tempSurface = SDL_LoadBMP("bitmaps/lightgreenFruit.bmp")) == NULL ){
CMessage::showformat("CFruit::OnInit - bitmapa nenactena {}",{"lightgreenFruit.bmp"});
return false;
}
tempTexture = SDL_CreateTextureFromSurface(Renderer,tempSurface);
FruitTextures.push_back(tempTexture);
if( (tempSurface = SDL_LoadBMP("bitmaps/purpleFruit.bmp")) == NULL ){
CMessage::showformat("CFruit::OnInit - bitmapa nenactena {}",{"purpleFruit.bmp"});
return false;
}
tempTexture = SDL_CreateTextureFromSurface(Renderer,tempSurface);
FruitTextures.push_back(tempTexture);
//----------------------------------------------------------------
SDL_FreeSurface(tempSurface);
return true;
}
//---------------------------------------------------------------------------
void CFruit::OnLoop(){
if(Eaten){
Spawn();
Eaten = false;
}
}
//---------------------------------------------------------------------------
void CFruit::OnRender(){
SDL_Rect dest = {CurrentFruit.X,CurrentFruit.Y,TILE_SIZE,TILE_SIZE};
SDL_Texture *tex = FruitTextures[currentTexture];
SDL_RenderCopy(Renderer,tex,NULL,&dest);
}
//---------------------------------------------------------------------------
void CFruit::Spawn(){
//random texture
currentTexture = rand() % 3;
//random coordinates
unsigned int X = (rand() %(WWIDTH-3*TILE_SIZE)) + 2*TILE_SIZE;
X = X - (X % TILE_SIZE);
unsigned int Y = (rand() %(WHEIGHT-3*TILE_SIZE)) + 2*TILE_SIZE;
Y = Y - (Y % TILE_SIZE);
CurrentFruit.X = X;
CurrentFruit.Y = Y;
}
//---------------------------------------------------------------------------