| 1 |
|
| 2 |
#ifdef HAVE_CONFIG_H |
| 3 |
#include <config.h> |
| 4 |
#endif |
| 5 |
|
| 6 |
#include <gettext.h> |
| 7 |
#define _(String) gettext (String) |
| 8 |
|
| 9 |
#include "AppFreeDink.h" |
| 10 |
#include "bgm.h" |
| 11 |
#include "dinkc_console.h" |
| 12 |
#include "dinkc_console_renderer.h" |
| 13 |
#include "debug.h" |
| 14 |
#include "debug_renderer.h" |
| 15 |
#include "game_engine.h" |
| 16 |
#include "live_sprites_manager.h" |
| 17 |
#include "editor_screen.h" |
| 18 |
#include "gfx.h" |
| 19 |
#include "IOGfxDisplay.h" |
| 20 |
#include "input.h" |
| 21 |
#include "log.h" |
| 22 |
#include "sfx.h" |
| 23 |
#include "game_choice.h" |
| 24 |
#include "update_frame.h" |
| 25 |
|
| 26 |
#include "SDL.h" |
| 27 |
|
| 28 |
|
| 29 |
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ |
| 30 |
#define WIN32_LEAN_AND_MEAN |
| 31 |
#include <windows.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
/* Text choice selection (e.g. "Load game" in the title screen) */ |
| 36 |
void freedink_update_mouse_text_choice(int dx, int dy) { |
| 37 |
play.mouse += dy; |
| 38 |
} |
| 39 |
void freedink_update_mouse(SDL_Event* ev) { |
| 40 |
if (ev->type != SDL_MOUSEMOTION) |
| 41 |
return; |
| 42 |
|
| 43 |
/* Players controls mouse */ |
| 44 |
if ((mode == 1 || keep_mouse) |
| 45 |
&& (spr[1].active && spr[1].brain == 13)) |
| 46 |
{ |
| 47 |
spr[1].x = ev->motion.x; |
| 48 |
spr[1].y = ev->motion.y; |
| 49 |
|
| 50 |
/* Clip the cursor to our client area */ |
| 51 |
if (spr[1].x > (640-1)) spr[1].x = 640-1; |
| 52 |
if (spr[1].y > (480-1)) spr[1].y = 480-1; |
| 53 |
if (spr[1].x < 0) spr[1].x = 0; |
| 54 |
if (spr[1].y < 0) spr[1].y = 0; |
| 55 |
} |
| 56 |
|
| 57 |
if (game_choice.active) |
| 58 |
freedink_update_mouse_text_choice(ev->motion.xrel, ev->motion.yrel); |
| 59 |
} |
| 60 |
|
| 61 |
/* Global game shortcuts */ |
| 62 |
static void freedink_input_global_shortcuts(SDL_Event* ev) { |
| 63 |
if (ev->type != SDL_KEYDOWN) |
| 64 |
return; |
| 65 |
if (ev->key.repeat) |
| 66 |
return; |
| 67 |
|
| 68 |
if (ev->key.keysym.scancode == SDL_SCANCODE_RETURN) { |
| 69 |
g_display->toggleFullScreen(); |
| 70 |
} else if (ev->key.keysym.sym == SDLK_q) { |
| 71 |
//shutdown game |
| 72 |
SDL_Event ev; |
| 73 |
ev.type = SDL_QUIT; |
| 74 |
SDL_PushEvent(&ev); |
| 75 |
} else if (ev->key.keysym.sym == SDLK_d) { |
| 76 |
/* Debug mode */ |
| 77 |
if (!debug_mode) |
| 78 |
log_debug_on(); |
| 79 |
else |
| 80 |
log_debug_off(); |
| 81 |
} else if (ev->key.keysym.sym == SDLK_c) { |
| 82 |
if (!console_active) |
| 83 |
dinkc_console_show(); |
| 84 |
else |
| 85 |
dinkc_console_hide(); |
| 86 |
} else if (ev->key.keysym.sym == SDLK_m) { |
| 87 |
//shutdown music |
| 88 |
StopMidi(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
void freedink_input_window(SDL_Event* ev) { |
| 93 |
if (ev->type == SDL_WINDOWEVENT) { |
| 94 |
if (ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { |
| 95 |
int w = ev->window.data1; |
| 96 |
int h = ev->window.data2; |
| 97 |
g_display->onSizeChange(w, h); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void log_touch(SDL_Event* ev) { |
| 103 |
char* desc; |
| 104 |
if (ev->type == SDL_FINGERUP) { |
| 105 |
desc = "SDL_FINGERUP"; |
| 106 |
} else if (ev->type == SDL_FINGERDOWN) { |
| 107 |
desc = "SDL_FINGERDOWN"; |
| 108 |
} else if (ev->type == SDL_FINGERMOTION) { |
| 109 |
desc = "SDL_FINGERMOTION"; |
| 110 |
} else { |
| 111 |
return; |
| 112 |
} |
| 113 |
SDL_TouchFingerEvent* tev = (SDL_TouchFingerEvent*)ev; |
| 114 |
log_debug("%s @%d id%" SDL_PRIs64 ",%" SDL_PRIs64 |
| 115 |
" (%f,%f) %f,%f p%f", |
| 116 |
desc, tev->timestamp, tev->touchId, tev->fingerId, |
| 117 |
tev->x, tev->y, tev->dx, tev->dy, tev->pressure); |
| 118 |
} |
| 119 |
|
| 120 |
void freedink_controls_renderer_render() { |
| 121 |
// SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); |
| 122 |
|
| 123 |
// SDL_RenderDrawLine(renderer, 0, 0, 100, 100); |
| 124 |
|
| 125 |
// SDL_Rect rectangle; |
| 126 |
// rectangle.x = 0; |
| 127 |
// rectangle.y = 0; |
| 128 |
// rectangle.w = 50; |
| 129 |
// rectangle.h = 50; |
| 130 |
// SDL_RenderFillRect(renderer, &rectangle); |
| 131 |
|
| 132 |
// SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
| 133 |
} |
| 134 |
|
| 135 |
AppFreeDink::AppFreeDink() { |
| 136 |
description = _("Starts the Dink Smallwood game or one of its D-Mods."); |
| 137 |
splash_path = "Tiles/Splash.bmp"; |
| 138 |
dinkini_playmidi = true; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* do work required for every instance of the application: create the |
| 143 |
* window, initialize data |
| 144 |
*/ |
| 145 |
void AppFreeDink::init() { |
| 146 |
/* Notify other apps that FreeDink is playing */ |
| 147 |
writeDinksmallwoodIni(true); |
| 148 |
|
| 149 |
/* Game-specific initialization */ |
| 150 |
/* Start with this initialization as it resets structures that are |
| 151 |
filled in other subsystems initialization */ |
| 152 |
game_init(opt_version); |
| 153 |
|
| 154 |
if (sound_on) |
| 155 |
bgm_init(); |
| 156 |
|
| 157 |
//Activate dink, but don't really turn him on |
| 158 |
//spr[1].active = TRUE; |
| 159 |
spr[1].timing = 33; |
| 160 |
|
| 161 |
// ** SETUP ** |
| 162 |
last_sprite_created = 1; |
| 163 |
set_mode(0); |
| 164 |
|
| 165 |
//lets run our init script |
| 166 |
int script = load_script("main", 0); |
| 167 |
locate(script, "main"); |
| 168 |
run_script(script); |
| 169 |
|
| 170 |
/* lets attach our vars to the scripts, they must be declared in the |
| 171 |
main.c DinkC script */ |
| 172 |
attach(); |
| 173 |
} |
| 174 |
|
| 175 |
AppFreeDink::~AppFreeDink() { |
| 176 |
bgm_quit(); |
| 177 |
game_quit(); |
| 178 |
writeDinksmallwoodIni(/*playing=*/0); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
void AppFreeDink::input(SDL_Event* ev) { |
| 183 |
// Show IME (virtual keyboard) on Android |
| 184 |
if (ev->type == SDL_KEYDOWN && ev->key.keysym.scancode == SDL_SCANCODE_MENU) { |
| 185 |
g_display->toggleScreenKeyboard(); |
| 186 |
} |
| 187 |
if (ev->type == SDL_KEYDOWN && ev->key.keysym.scancode == SDL_SCANCODE_AC_BACK) { |
| 188 |
// quit for now |
| 189 |
SDL_Event ev; |
| 190 |
ev.type = SDL_QUIT; |
| 191 |
SDL_PushEvent(&ev); |
| 192 |
} |
| 193 |
|
| 194 |
freedink_input_window(ev); |
| 195 |
log_touch(ev); |
| 196 |
touchConv.convertInput(ev); |
| 197 |
if (ev->type == SDL_KEYUP |
| 198 |
&& input_getscancodestate(ev->key.keysym.scancode) == SDL_PRESSED) { |
| 199 |
// always tell the game when the player releases a key |
| 200 |
input_update(ev); |
| 201 |
} else if ((ev->type == SDL_KEYDOWN || ev->type == SDL_KEYUP) |
| 202 |
&& (ev->key.keysym.mod & KMOD_ALT)) { |
| 203 |
freedink_input_global_shortcuts(ev); |
| 204 |
} else if (console_active) { |
| 205 |
dinkc_console_process_key(ev); |
| 206 |
} else { |
| 207 |
// forward all events to the game |
| 208 |
input_update(ev); |
| 209 |
} |
| 210 |
|
| 211 |
// Process mouse even in console mode |
| 212 |
freedink_update_mouse(ev); |
| 213 |
} |
| 214 |
|
| 215 |
void AppFreeDink::logic() { |
| 216 |
#ifndef __EMSCRIPTEN__ |
| 217 |
// TODO: fine-tune framerate from emscripten - should mostly be 60FPS as we want *for 1.08* |
| 218 |
SDL_framerateDelay(&framerate_manager); |
| 219 |
#endif |
| 220 |
debug_logic(); |
| 221 |
updateFrame(); |
| 222 |
|
| 223 |
/* Renderers */ |
| 224 |
if (!abort_this_flip) { |
| 225 |
g_display->clear(); |
| 226 |
debug_renderer_render(); |
| 227 |
dinkc_console_renderer_render(); |
| 228 |
g_display->flipStretch(IOGFX_backbuffer); // game area |
| 229 |
freedink_controls_renderer_render(); // TODO: always display me on flip_it |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Save where Dink is installed in a .ini file, read by third-party |
| 235 |
* applications like the DinkFrontEnd. Also notify whether Dink is |
| 236 |
* running or not. |
| 237 |
*/ |
| 238 |
void AppFreeDink::writeDinksmallwoodIni(bool playing) { |
| 239 |
if (g_b_no_write_ini) |
| 240 |
return; //fix problem with NT security if -noini is set |
| 241 |
/* TODO: saves it in the user home instead. */ |
| 242 |
|
| 243 |
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ |
| 244 |
char windir[MAX_PATH]; |
| 245 |
char inifile[MAX_PATH]; |
| 246 |
GetWindowsDirectory(windir, MAX_PATH); |
| 247 |
sprintf(inifile, "%s\\dinksmallwood.ini", windir); |
| 248 |
|
| 249 |
unlink(inifile); |
| 250 |
|
| 251 |
FILE* f = fopen(inifile, "w"); |
| 252 |
if (f == NULL) { |
| 253 |
log_error("Couldn't write dinksmallwood.ini: %s", strerror(errno)); |
| 254 |
return; |
| 255 |
} |
| 256 |
fprintf(f, "[Dink Smallwood Directory Information for the CD to read]\n"); |
| 257 |
fprintf(f, "%s", SDL_GetBasePath()); |
| 258 |
fprintf(f, "\n"); |
| 259 |
if (playing) |
| 260 |
fprintf(f, "TRUE\n"); |
| 261 |
else |
| 262 |
fprintf(f, "FALSE\n"); |
| 263 |
fclose(f); |
| 264 |
#endif |
| 265 |
} |