forked from Vogtinator/crafti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.cpp
146 lines (117 loc) · 3.32 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
#include "task.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);
}
static constexpr int savefile_version = 5;
#define LOAD_FROM_FILE(var) if(fread(&var, sizeof(var), 1, file) != 1) { fclose(file); return false; }
#define SAVE_TO_FILE(var) if(fwrite(&var, sizeof(var), 1, file) != 1) { fclose(file); return false; }
bool Task::load()
{
FILE *file = fopen(savefile, "rb");
if(!file)
return false;
int version;
LOAD_FROM_FILE(version);
//For backwards compatibility
if(version == savefile_version)
{
if(!settings_task.loadFromFile(file))
{
fclose(file);
return false;
}
}
else if(version != 4)
{
printf("Wrong save file version %d!\n", version);
fclose(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)
LOAD_FROM_FILE(current_inventory.current_slot)
LOAD_FROM_FILE(block_list_task.current_selection)
const bool ret = world.loadFromFile(file);
fclose(file);
world.setPosition(world_task.x, world_task.y, world_task.z);
return ret;
}
bool Task::save()
{
FILE *file = fopen(savefile, "wb");
if(!file)
return false;
SAVE_TO_FILE(savefile_version)
if(!settings_task.saveToFile(file))
{
fclose(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);
fclose(file);
return ret;
}