summaryrefslogtreecommitdiffstats
path: root/game.cpp
blob: 5090746041401bfdfb3349069006226f78c7d907 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "display.h"
#include "music.h"

std::vector<displaynote> load_notes()
{
	int difficulty = 0;
	int current_note = 0;
	int current_line = 0;
	int current_mes = 0;

	std::vector<displaynote> displayednotes;
	int ms=0;
	while(current_mes<=songData->songNotes[difficulty].noteData.size())
	{
		std::vector<Note> note = songData->songNotes[difficulty].noteData[current_mes][current_line];

		float time = 1000.0f/(songData->bpms[0].bpm/60.0f);

		if(songData->songNotes[difficulty].noteData[current_mes].size() != 4)
			time = time/((float)songData->songNotes[difficulty].noteData[current_mes].size()/4.0f);

		for(std::vector<displaynote>::iterator current_note = note.begin(); show_note != note.end(); ++current_note) {
			if(note[0] == Tap)
			{
				currentNote.orientation = "left";
				displayednotes[ms]=currentNote;
			}
			if(note[1] == Tap)
			{
				currentNote.orientation = "up";
				displayednotes[ms]=currentNote;
			}
			if(note[2] == Tap)
			{
				currentNote.orientation = "down";
				displayednotes[ms]=currentNote;
			}
			if(note[3] == Tap)
			{
				currentNote.orientation = "right";
				currentNote.x = 0.5f - (float)rightW / (float)(SCREEN_WIDTH*2);
				currentNote.y = 0.5f - (float)rightH / (float)(SCREEN_HEIGHT*2);
				displayednotes[ms]=currentNote;
			}
		}
		current_line += 1;
			
		if(songData->songNotes[difficulty].noteData[current_mes].size() <= current_line)
		{
			current_line = 0;
			current_mes +=1;
		}
		ms+=time;
	}
	return displayednotes;
}

int init_game()
{
	unsigned int last_tick = SDL_GetTicks();
	unsigned int last_update = 0;
	std::vector<displaynote> displayednotes = load_notes();
	displaynote currentNote;

	SDL_Texture *arrow;

	musique = Mix_LoadMUS(path.c_str());
	if(musique == nullptr)
		logMixError(std::cout, "LoadMUS");

	arrow = loadTexture("/sdcard/up_arrow.png", screen.renderer);

	int arrowW, arrowH;
	SDL_QueryTexture(arrow, NULL, NULL, &arrowW, &arrowH);

	unsigned int first_tick = SDL_GetTicks();
	float song_offset = (((0.4f/speed)*20.0f)+songData->offset*1000.0f);
	SDL_Log("offset: %f", song_offset);
}

int update_game()
{

		if(((SDL_GetTicks() - first_tick) >= (int)song_offset) && !playing)
		{
			if((musique != nullptr) && (Mix_PlayMusic(musique, 0) == -1))	
			{
				logMixError(std::cout, "PlayMusic");
				quit = true;
			}
			else
			{
				first_tick = SDL_GetTicks();
				playing = true;
			}
		}

		if(SDL_GetTicks() - last_update >= 20)
		{
			last_update = SDL_GetTicks();
			SDL_RenderPresent(renderer);

			SDL_RenderClear(renderer);
		        renderTexture(background, renderer, 0.0f, 0.0f);
		       	renderTexture(background, renderer, bW, 0.0f);
		       	renderTexture(background, renderer, 0.0f, bH);
		       	renderTexture(background, renderer, bW, bH);

			SDL_SetTextureColorMod(left, 0, 0, 255);
			SDL_SetTextureColorMod(up, 0, 0, 255);
			SDL_SetTextureColorMod(down, 0, 0, 255);
			SDL_SetTextureColorMod(right, 0, 0, 255);

			renderTexture(left, renderer, 0.1f - (float)leftW / (float)(SCREEN_WIDTH*2), 0.5f - (float)leftH / (float)(SCREEN_HEIGHT*2));
			renderTexture(up, renderer, 0.5f - (float)upW / (float)(SCREEN_WIDTH*2), 0.9f - (float)upH / (float)(SCREEN_HEIGHT*2));
			renderTexture(down, renderer, 0.5f - (float)downW / (float)(SCREEN_WIDTH*2), 0.1f - (float)downH / (float)(SCREEN_HEIGHT*2));
			renderTexture(right, renderer, 0.9f - (float)rightW / (float)(SCREEN_WIDTH*2), 0.5f - (float)rightH / (float)(SCREEN_HEIGHT*2));

			SDL_SetTextureColorMod(left, 255, 255, 255);
			SDL_SetTextureColorMod(up, 255, 255, 255);
			SDL_SetTextureColorMod(down, 255, 255, 255);
			SDL_SetTextureColorMod(right, 255, 255, 255);

			for(std::vector<displaynote>::iterator show_note = displayednotes.begin(); show_note != displayednotes.end(); ++show_note) {
				bool deleted = false;
				if(show_note->orientation != "center")
				for(std::vector<SDL_TouchFingerEvent>::iterator touch = touchEvents.begin(); touch != touchEvents.end(); ++touch) {

					if( ((show_note->orientation == "left") &&
						(show_note->x >= 0.05f && show_note->x <= 0.15f && show_note->y == 0.5f - (float)leftH / (float)(SCREEN_HEIGHT*2)) &&
						(touch->x >= 0.0f && touch->x <= 0.25f && touch->y >= 0.20f && touch->y <= 0.80f)) ||
					((show_note->orientation == "up") &&
						(show_note->x == 0.5f - (float)upW / (float)(SCREEN_WIDTH*2) && show_note->y >= 0.05f && show_note->y <= 0.15f) &&
						(touch->x >= 0.20f && touch->x <= 0.80f && touch->y >= 0.0f && touch->y <= 0.25f)) ||
					((show_note->orientation == "down") &&
						(show_note->x == 0.5f - (float)downW / (float)(SCREEN_WIDTH*2) && show_note->y >= 0.85f && show_note->y <= 0.95f) &&
						(touch->x >= 0.20f && touch->x <= 0.80f && touch->y >= 1.0f && touch->y <= 0.75f)) ||
					((show_note->orientation == "right") &&
						(show_note->x >= 0.85f && show_note->x <= 0.95f && show_note->y == 0.5f - (float)rightH / (float)(SCREEN_HEIGHT*2)) &&
						(touch->x >= 0.75f && touch->x <= 1.0f && touch->y >= 0.20f && touch->y <= 0.80f)))
					{

						currentNote.texture = image;
						currentNote.orientation = "center";
						currentNote.x = iW;
						currentNote.y = iH;
						displayednotes.push_back(currentNote);

						SDL_SetTextureColorMod(show_note->texture, 255, 0, 0);
						renderTexture(show_note->texture, renderer, show_note->x, show_note->y);
						SDL_SetTextureColorMod(show_note->texture, 255, 255, 255);
						touchEvents.erase(touch);
						displayednotes.erase(show_note);
						deleted = true;
						break;
					}
				}

				if(!deleted)
				{
					if(show_note->x <= 0.0f || show_note->y <= 0.0f)
					{
						displayednotes.erase(show_note);
						continue;
					}
					renderTexture(show_note->texture, renderer, show_note->x, show_note->y);

					if(show_note->orientation == "left")
						show_note->x -= speed;
					if(show_note->orientation == "up")
						show_note->y -= speed;
					if(show_note->orientation == "down")
						show_note->y += speed;
					if(show_note->orientation == "right")
						show_note->x += speed;
					if(show_note->orientation == "center")
					{
						show_note->y += speed;
						show_note->x += speed;
					}
				}
			}

			touchEvents.clear();
		}
}

int quit_game()
{
	SDL_DestroyTexture(background);
	SDL_DestroyTexture(image);

	SDL_Log("Music was playing");
	Mix_HaltMusic();
	SDL_Log("Music halted");
	Mix_FreeMusic(musique);
	musique = NULL;
	SDL_Log("Music freed");
}