| 1 |
|
| 2 |
#if defined _DEBUG && defined _MSC_VER |
| 3 |
#include <crtdbg.h> |
| 4 |
#endif |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdint.h> |
| 9 |
#include <stdbool.h> |
| 10 |
#include <math.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#ifdef _WIN32 |
| 13 |
#define WIN32_MEAN_AND_LEAN |
| 14 |
#include <windows.h> |
| 15 |
#else |
| 16 |
#include <unistd.h> |
| 17 |
#endif |
| 18 |
#include <SDL2/SDL.h> |
| 19 |
#include "config.h" |
| 20 |
#include "palette.h" |
| 21 |
#include "gui.h" |
| 22 |
#include "hpc.h" |
| 23 |
|
| 24 |
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) |
| 25 |
|
| 26 |
#ifdef _MSC_VER |
| 27 |
#pragma warning(disable: 4204) |
| 28 |
#pragma warning(disable: 4221) |
| 29 |
#endif |
| 30 |
|
| 31 |
static hpc_t vblankHpc; |
| 32 |
static SDL_Window *window; |
| 33 |
static SDL_Renderer *renderer; |
| 34 |
static SDL_Texture *texture; |
| 35 |
|
| 36 |
|
| 37 |
volatile bool programRunning, redrawScreen, allowSigterm = true; |
| 38 |
uint32_t *frameBuffer; |
| 39 |
keyb_t keyb; |
| 40 |
mouse_t mouse; |
| 41 |
|
| 42 |
static bool setupVideo(void); |
| 43 |
static void readMouseXY(void); |
| 44 |
static void handleInput(void); |
| 45 |
|
| 46 |
#ifdef __APPLE__ |
| 47 |
static void osxSetDirToProgramDirFromArgs(char **argv); |
| 48 |
#endif |
| 49 |
|
| 50 |
int main(int argc, char *argv[]) |
| 51 |
{ |
| 52 |
#if defined _WIN32 || defined __APPLE__ |
| 53 |
SDL_version sdlVer; |
| 54 |
#endif |
| 55 |
|
| 56 |
|
| 57 |
#if defined _DEBUG && defined _MSC_VER |
| 58 |
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); |
| 59 |
#endif |
| 60 |
|
| 61 |
(void)argc; |
| 62 |
(void)argv; |
| 63 |
|
| 64 |
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); |
| 65 |
SDL_EnableScreenSaver(); |
| 66 |
|
| 67 |
|
| 68 |
#if defined _WIN32 || defined __APPLE__ |
| 69 |
SDL_GetVersion(&sdlVer); |
| 70 |
if (sdlVer.major != SDL_MAJOR_VERSION || sdlVer.minor != SDL_MINOR_VERSION || sdlVer.patch != SDL_PATCHLEVEL) |
| 71 |
{ |
| 72 |
#ifdef _WIN32 |
| 73 |
showErrorMsgBox("SDL2.dll is not the expected version, the program will terminate.\n\n" \ |
| 74 |
"Loaded dll version: %d.%d.%d\n" \ |
| 75 |
"Required (compiled with) version: %d.%d.%d", |
| 76 |
sdlVer.major, sdlVer.minor, sdlVer.patch, |
| 77 |
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL); |
| 78 |
#else |
| 79 |
showErrorMsgBox("The loaded SDL2 library is not the expected version, the program will terminate.\n\n" \ |
| 80 |
"Loaded library version: %d.%d.%d\n" \ |
| 81 |
"Required (compiled with) version: %d.%d.%d", |
| 82 |
sdlVer.major, sdlVer.minor, sdlVer.patch, |
| 83 |
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL); |
| 84 |
#endif |
| 85 |
return 0; |
| 86 |
} |
| 87 |
#endif |
| 88 |
|
| 89 |
#ifdef _WIN32 |
| 90 |
#ifndef _MSC_VER |
| 91 |
SetProcessDPIAware(); |
| 92 |
#endif |
| 93 |
#endif |
| 94 |
|
| 95 |
#ifdef __APPLE__ |
| 96 |
osxSetDirToProgramDirFromArgs(argv); |
| 97 |
#endif |
| 98 |
|
| 99 |
if (!setupVideo()) |
| 100 |
{ |
| 101 |
SDL_Quit(); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
SDL_StopTextInput(); |
| 106 |
|
| 107 |
hpc_Init(); |
| 108 |
hpc_SetDurationInHz(&vblankHpc, 60); |
| 109 |
|
| 110 |
setupGUI(); |
| 111 |
redrawScreen = true; |
| 112 |
|
| 113 |
programRunning = true; |
| 114 |
|
| 115 |
hpc_ResetCounters(&vblankHpc); |
| 116 |
while (programRunning) |
| 117 |
{ |
| 118 |
readMouseXY(); |
| 119 |
handleInput(); |
| 120 |
|
| 121 |
if (redrawScreen) |
| 122 |
{ |
| 123 |
redrawScreen = false; |
| 124 |
|
| 125 |
|
| 126 |
SDL_UpdateTexture(texture, NULL, frameBuffer, SCREEN_W * sizeof (int32_t)); |
| 127 |
SDL_RenderClear(renderer); |
| 128 |
SDL_RenderCopy(renderer, texture, NULL, NULL); |
| 129 |
SDL_RenderPresent(renderer); |
| 130 |
} |
| 131 |
|
| 132 |
hpc_Wait(&vblankHpc); |
| 133 |
} |
| 134 |
|
| 135 |
if (loadedFile != NULL) free(loadedFile); |
| 136 |
free(frameBuffer); |
| 137 |
SDL_Quit(); |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
static void readMouseXY(void) |
| 143 |
{ |
| 144 |
int32_t windowX, windowY, mx, my; |
| 145 |
|
| 146 |
SDL_GetGlobalMouseState(&mx, &my); |
| 147 |
SDL_GetWindowPosition(window, &windowX, &windowY); |
| 148 |
|
| 149 |
mx -= windowX; |
| 150 |
my -= windowY; |
| 151 |
|
| 152 |
mouse.x = (int32_t)(mx * mouse.fMouseXMul); |
| 153 |
mouse.y = (int32_t)(my * mouse.fMouseYMul); |
| 154 |
} |
| 155 |
|
| 156 |
static void showAskToSaveDialog(void) |
| 157 |
{ |
| 158 |
int32_t whichButtonPressed; |
| 159 |
|
| 160 |
const SDL_MessageBoxButtonData buttons[2] = |
| 161 |
{ |
| 162 |
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "No" }, |
| 163 |
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "Yes" }, |
| 164 |
}; |
| 165 |
|
| 166 |
const SDL_MessageBoxData messageBoxData = |
| 167 |
{ |
| 168 |
SDL_MESSAGEBOX_WARNING, |
| 169 |
window, |
| 170 |
"Warning", |
| 171 |
"Colors are unsaved. Save before quitting?", |
| 172 |
SDL_arraysize(buttons), |
| 173 |
buttons, |
| 174 |
NULL |
| 175 |
}; |
| 176 |
|
| 177 |
if (SDL_ShowMessageBox(&messageBoxData, &whichButtonPressed) < 0) |
| 178 |
{ |
| 179 |
allowSigterm = true; |
| 180 |
programRunning = false; |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
if (whichButtonPressed == 1) |
| 185 |
savePalette(0); |
| 186 |
|
| 187 |
programRunning = false; |
| 188 |
} |
| 189 |
|
| 190 |
void showErrorMsgBox(const char *fmt, ...) |
| 191 |
{ |
| 192 |
char strBuf[1024]; |
| 193 |
va_list args; |
| 194 |
|
| 195 |
|
| 196 |
va_start(args, fmt); |
| 197 |
vsnprintf(strBuf, sizeof (strBuf), fmt, args); |
| 198 |
va_end(args); |
| 199 |
|
| 200 |
|
| 201 |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", strBuf, NULL); |
| 202 |
} |
| 203 |
|
| 204 |
static void handleInput(void) |
| 205 |
{ |
| 206 |
SDL_Event inputEvent; |
| 207 |
|
| 208 |
while (SDL_PollEvent(&inputEvent)) |
| 209 |
{ |
| 210 |
if (inputEvent.type == SDL_QUIT && allowSigterm) |
| 211 |
{ |
| 212 |
if (!configIsSaved) |
| 213 |
{ |
| 214 |
allowSigterm = false; |
| 215 |
showAskToSaveDialog(); |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
programRunning = false; |
| 220 |
} |
| 221 |
} |
| 222 |
else if (inputEvent.type == SDL_KEYDOWN) |
| 223 |
{ |
| 224 |
keyDownHandler(inputEvent.key.keysym.sym); |
| 225 |
} |
| 226 |
else if (inputEvent.type == SDL_MOUSEBUTTONUP) |
| 227 |
{ |
| 228 |
if (inputEvent.button.button == SDL_BUTTON_LEFT) |
| 229 |
mouseButtonUpHandler(); |
| 230 |
} |
| 231 |
else if (inputEvent.type == SDL_MOUSEBUTTONDOWN) |
| 232 |
{ |
| 233 |
if (inputEvent.button.button == SDL_BUTTON_LEFT) |
| 234 |
mouseButtonDownHandler(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
handleSlidersHeldDown(); |
| 239 |
handleRainbowHeldDown(); |
| 240 |
handleRainbowUpDownButtons(); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
#ifdef __APPLE__ |
| 245 |
static void osxSetDirToProgramDirFromArgs(char **argv) |
| 246 |
{ |
| 247 |
char *tmpPath; |
| 248 |
int32_t i, tmpPathLen; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
if (argv[0] != NULL && argv[0][0] == '/') |
| 255 |
{ |
| 256 |
tmpPath = strdup(argv[0]); |
| 257 |
if (tmpPath != NULL) |
| 258 |
{ |
| 259 |
|
| 260 |
tmpPathLen = strlen(tmpPath); |
| 261 |
for (i = tmpPathLen-1; i >= 0; i--) |
| 262 |
{ |
| 263 |
if (tmpPath[i] == '/') |
| 264 |
{ |
| 265 |
tmpPath[i] = '\0'; |
| 266 |
break; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
chdir(tmpPath); |
| 271 |
chdir("../../../"); |
| 272 |
|
| 273 |
free(tmpPath); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
#endif |
| 278 |
|
| 279 |
static bool setupVideo(void) |
| 280 |
{ |
| 281 |
int32_t renderW, renderH; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
#if defined _WIN32 && SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL == 9 |
| 289 |
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0) |
| 290 |
#else |
| 291 |
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) != 0) |
| 292 |
#endif |
| 293 |
{ |
| 294 |
showErrorMsgBox("Couldn't initialize SDL: %s", SDL_GetError()); |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
window = SDL_CreateWindow("Palette editor for ProTracker 2 clone", |
| 299 |
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_W * 2, SCREEN_H * 2, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); |
| 300 |
|
| 301 |
if (window == NULL) |
| 302 |
{ |
| 303 |
showErrorMsgBox("Couldn't create SDL window:\n%s", SDL_GetError()); |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
renderer = SDL_CreateRenderer(window, -1, 0); |
| 308 |
if (renderer == NULL) |
| 309 |
{ |
| 310 |
showErrorMsgBox("Couldn't create SDL renderer:\n%s\n\n" \ |
| 311 |
"Is your GPU (+ driver) too old?", SDL_GetError()); |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
SDL_RenderSetLogicalSize(renderer, SCREEN_W, SCREEN_H); |
| 316 |
|
| 317 |
#if SDL_MINOR_VERSION >= 24 || (SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL >= 5) |
| 318 |
SDL_RenderSetIntegerScale(renderer, SDL_TRUE); |
| 319 |
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); |
| 320 |
#endif |
| 321 |
|
| 322 |
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); |
| 323 |
|
| 324 |
SDL_SetHint("SDL_RENDER_SCALE_QUALITY", "nearest"); |
| 325 |
|
| 326 |
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_W, SCREEN_H); |
| 327 |
if (texture == NULL) |
| 328 |
{ |
| 329 |
showErrorMsgBox("Couldn't create an SDL2 texture: %s", SDL_GetError()); |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE); |
| 334 |
|
| 335 |
|
| 336 |
frameBuffer = (uint32_t *)malloc(SCREEN_W * SCREEN_H * sizeof (int32_t)); |
| 337 |
if (frameBuffer == NULL) |
| 338 |
{ |
| 339 |
showErrorMsgBox("Out of memory!"); |
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
SDL_GetWindowSize(window, &renderW, &renderH); |
| 344 |
if (renderW > 0) mouse.fMouseXMul = (float)SCREEN_W / renderW; |
| 345 |
if (renderH > 0) mouse.fMouseYMul = (float)SCREEN_H / renderH; |
| 346 |
|
| 347 |
return true; |
| 348 |
} |