forked from Vogtinator/crafti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.cpp
164 lines (132 loc) · 4.12 KB
/
task.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
#include "task.h"
#include <zlib.h>
#include "texturetools.h"
#include "blocklisttask.h"
#include "worldtask.h"
#include "settingstask.h"
#include "inventory.h"
//The values have to stay somewhere
Task *Task::current_task;
bool Task::key_held_down, Task::running, Task::background_saved, Task::has_touchpad, Task::keys_inverted;
TEXTURE *Task::screen, *Task::background;
const char *Task::savefile;
void Task::makeCurrent()
{
current_task = this;
}
bool Task::keyPressed(const t_key &key)
{
#ifdef _TINSPIRE
if(has_touchpad)
{
if(key.tpad_arrow != TPAD_ARROW_NONE)
return touchpad_arrow_pressed(key.tpad_arrow);
else
return !(*reinterpret_cast<volatile uint16_t*>(0x900E0000 + key.tpad_row) & key.tpad_col) == keys_inverted;
}
else
return (*reinterpret_cast<volatile uint16_t*>(0x900E0000 + key.row) & key.col) == 0;
#else
return false;
#endif
}
void Task::initializeGlobals(const char *savefile)
{
running = true;
screen = newTexture(SCREEN_WIDTH, SCREEN_HEIGHT);
nglSetBuffer(screen->bitmap);
has_touchpad = is_touchpad;
keys_inverted = is_classic;
background = newTexture(SCREEN_WIDTH, SCREEN_HEIGHT);
background_saved = false;
Task::savefile = savefile;
}
void Task::deinitializeGlobals()
{
deleteTexture(screen);
deleteTexture(background);
}
void Task::saveBackground()
{
copyTexture(*screen, *background);
background_saved = true;
}
void Task::drawBackground()
{
copyTexture(*background, *screen);
}
/* Version 2: First in git
* Version 3 (31d5ee3a): Blocks are stored as 16bit BLOCK_WDATA
* Version 4 (1ebc685a): Add inventory
* Version 5 (710e7269): Add settings
* Version 6 (d52f3992): BLOCK_SIZE changed from 120 to 128,
* gzip compression introduced shortly afterwards
*/
static constexpr int savefile_version = 6;
#define LOAD_FROM_FILE(var) if(gzfread(&var, sizeof(var), 1, file) != 1) { gzclose(file); return false; }
#define SAVE_TO_FILE(var) if(gzfwrite(&var, sizeof(var), 1, file) != 1) { gzclose(file); return false; }
bool Task::load()
{
// Versions before 6 (and 6 for a short time) were uncompressed,
// but gzopen detects and handles uncompressed files transparently.
// Previous versions read the gzip magic as savefile version and bail out.
gzFile file = gzopen(savefile, "rb");
if(!file)
return false;
int version;
LOAD_FROM_FILE(version);
static_assert(savefile_version == 6, "Adjust loading code for backward compatibility");
if(version < 4 || version > 6)
{
printf("Save file version %d not supported!\n", version);
gzclose(file);
return false;
}
if(!settings_task.loadFromFile(file, version))
{
gzclose(file);
return false;
}
LOAD_FROM_FILE(current_inventory.entries)
LOAD_FROM_FILE(world_task.xr)
LOAD_FROM_FILE(world_task.yr)
LOAD_FROM_FILE(world_task.x)
LOAD_FROM_FILE(world_task.y)
LOAD_FROM_FILE(world_task.z)
// Previous versions used BLOCK_SIZE 120
if(version < 6)
{
world_task.x = world_task.x * BLOCK_SIZE / 120;
world_task.y = world_task.y * BLOCK_SIZE / 120;
world_task.z = world_task.z * BLOCK_SIZE / 120;
}
LOAD_FROM_FILE(current_inventory.current_slot)
LOAD_FROM_FILE(block_list_task.current_selection)
const bool ret = world.loadFromFile(file);
gzclose(file);
world.setPosition(world_task.x, world_task.y, world_task.z);
return ret;
}
bool Task::save()
{
gzFile file = gzopen(savefile, "wb");
if(!file)
return false;
SAVE_TO_FILE(savefile_version)
if(!settings_task.saveToFile(file))
{
gzclose(file);
return false;
}
SAVE_TO_FILE(current_inventory.entries)
SAVE_TO_FILE(world_task.xr)
SAVE_TO_FILE(world_task.yr)
SAVE_TO_FILE(world_task.x)
SAVE_TO_FILE(world_task.y)
SAVE_TO_FILE(world_task.z)
SAVE_TO_FILE(current_inventory.current_slot)
SAVE_TO_FILE(block_list_task.current_selection)
const bool ret = world.saveToFile(file);
gzclose(file);
return ret;
}