generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsokoblit.cpp
193 lines (159 loc) · 4.41 KB
/
sokoblit.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
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
/*
* sokoblit.cpp - part of SokoBlit
*
* Copyright (c) 2021 Pete Favelle / fsqaured limited <[email protected]>
*
* Entry point to the game; this is also where we declare a few helper functions
* which, if I was doing this properly, would be tidily wrapped up in some sort
* of utility singleton.
* But I'm not.
*
* This software is distributed under the MIT License. See LICENSE for details.
*/
/* System headers. */
/* Local headers. */
#include "32blit.hpp"
#include "sokoblit.hpp"
#include "Game.hpp"
#include "Menu.hpp"
/* Global variables (yes, I know...) */
Game *g_game = nullptr;
Menu *g_menu = nullptr;
uimode_t g_mode = MODE_MENU;
uint8_t g_level = 1;
uint8_t g_zoom = 100;
/* Functions. */
/*
* level_centre - works out the centre of a level, in world co-ordinates. Done
* here so everything has a common view about it!
*/
blit::Point level_centre( uint8_t p_level )
{
blit::Point l_point = blit::Point( 0, 0 );
/* The first 10 levels are pretty easy. */
if ( p_level >= 1 && p_level <= 5 )
{
l_point.x = ( blit::screen.bounds.w * p_level ) - ( blit::screen.bounds.w / 2 );
l_point.y = blit::screen.bounds.h * 0.5;
}
if ( p_level >= 6 && p_level <= 10 )
{
l_point.x = ( blit::screen.bounds.w * ( p_level - 5 ) ) - ( blit::screen.bounds.w / 2 );
l_point.y = blit::screen.bounds.h * 1.5;
}
/* And the last 10, too */
if ( p_level >= 13 && p_level <= 17 )
{
l_point.x = ( blit::screen.bounds.w * ( p_level - 12 ) ) - ( blit::screen.bounds.w / 2 );
l_point.y = blit::screen.bounds.h * 3.5;
}
if ( p_level >= 18 && p_level <= 22 )
{
l_point.x = ( blit::screen.bounds.w * ( p_level - 17 ) ) - ( blit::screen.bounds.w / 2 );
l_point.y = blit::screen.bounds.h * 4.5;
}
/* The side middle two.... we'll just hard code. */
if ( 11 == p_level )
{
l_point.x = blit::screen.bounds.w * 0.5;
l_point.y = blit::screen.bounds.h * 2.5;
}
if ( 12 == p_level )
{
l_point.x = blit::screen.bounds.w * 4.5;
l_point.y = blit::screen.bounds.h * 2.5;
}
/* And return what we worked out. */
return l_point;
}
/*
* init - called when the game is launched, we create our globals, initialise
* the screen and that sort of thing.
*/
void init( void )
{
/* Switch into hires mode, please. */
blit::set_screen_mode( blit::ScreenMode::hires );
/* Create the menu and game objects that handle everything. */
g_menu = new Menu();
g_game = new Game();
}
/*
* render - called every frame to draw the world view
*/
void render( uint32_t p_time )
{
/* Clear the screen down, so that whichever render does the work gets */
/* a clean slate to work from. */
blit::screen.pen = blit::Pen( 0, 0, 0 );
blit::screen.clear();
/* Work out which tilemap(s) we should render, and render them. */
if ( MODE_GAME != g_mode )
{
if ( nullptr != g_menu )
{
g_menu->render( p_time, g_zoom );
}
}
if ( nullptr != g_game )
{
g_game->render( p_time, g_zoom );
}
/* All done */
return;
}
/*
* update - called every 10ms to update the world view.
*/
void update( uint32_t p_time )
{
/* If we're transitioning, update the progress. */
if ( MODE_TO_GAME == g_mode )
{
/* Decrease progress, and if we hit zero we've reached GAME. */
if ( 0 == --g_zoom )
{
g_mode = MODE_GAME;
}
}
if ( MODE_TO_MENU == g_mode )
{
/* Increase progress, and if we hit 100 we've reached MENU. */
if ( 100 == ++g_zoom )
{
g_mode = MODE_MENU;
}
}
/* Check the menu button, which is a universal toggle. */
if ( ( blit::buttons.pressed & blit::Button::MENU ) ||
( blit::buttons.pressed & blit::Button::A ) )
{
/* Only acts if we're in a steady state. */
if ( MODE_MENU == g_mode )
{
g_mode = MODE_TO_GAME;
}
if ( MODE_GAME == g_mode )
{
g_mode = MODE_TO_MENU;
}
}
/* Only bother updating object that are active. */
if ( MODE_GAME != g_mode )
{
if ( nullptr != g_menu )
{
g_menu->update( p_time );
}
}
if ( MODE_MENU != g_mode )
{
if ( nullptr != g_game )
{
g_game->update( p_time );
}
}
/* All done. */
return;
}
/* End of file sokoblit.cpp */