Debian Codegraph @

src/pt2_visuals.c

raw symbols SCIP
1 // for finding memory leaks in debug mode with Visual Studio
2 #if defined _DEBUG && defined _MSC_VER
3 #include <crtdbg.h>
4 #endif
5
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <math.h> // modf()
11 #ifdef _WIN32
12 #define WIN32_MEAN_AND_LEAN
13 #include <windows.h>
14 #include <SDL2/SDL_syswm.h>
15 #endif
16 #include <stdint.h>
17 #include <stdbool.h>
18 #ifndef _WIN32
19 #include <unistd.h> // usleep()
20 #endif
21 #include <ctype.h> // tolower()
22 #include "pt2_helpers.h"
23 #include "pt2_textout.h"
24 #include "pt2_tables.h"
25 #include "pt2_pattern_viewer.h"
26 #include "pt2_sampler.h"
27 #include "pt2_diskop.h"
28 #include "pt2_visuals.h"
29 #include "pt2_scopes.h"
30 #include "pt2_edit.h"
31 #include "pt2_config.h"
32 #include "pt2_bmp.h"
33 #include "pt2_sampling.h"
34 #include "pt2_chordmaker.h"
35 #include "pt2_mod2wav.h"
36 #include "pt2_audio.h"
37 #include "pt2_posed.h"
38 #include "pt2_textedit.h"
39
40 typedef struct sprite_t
41 {
42 bool visible;
43 int8_t pixelType;
44 int16_t newX, newY, x, y, w, h;
45 uint32_t colorKey, *refreshBuffer;
46 const void *data;
47 } sprite_t;
48
49 // for debug box
50 #define FPS_SCAN_FRAMES 60
51 static char fpsTextBuf[1024];
52 static bool avgFramesReady;
53 static uint32_t videoFrameCounter;
54 static uint64_t frameStartTime, runningFrameDuration;
55 static double dFrameDurationDiv, dAvgFPS;
56 // ------------------
57
58 static int32_t oldCurrMode = -1;
59 static uint32_t vuMetersBg[4 * (10 * 48)];
60
61 sprite_t sprites[SPRITE_NUM]; // globalized
62
63 static const uint16_t cursorPosTable[24] =
64 {
65 30, 54, 62, 70, 78, 86,
66 102, 126, 134, 142, 150, 158,
67 174, 198, 206, 214, 222, 230,
68 246, 270, 278, 286, 294, 302
69 };
70
71 void updateSongInfo1(void);
72 void updateSongInfo2(void);
73 void updateSampler(void);
74 void updatePatternData(void);
75
76 void blit32(int32_t x, int32_t y, int32_t w, int32_t h, const uint32_t *src)
77 {
78 const uint32_t *srcPtr = src;
79 uint32_t *dstPtr = &video.frameBuffer[(y * SCREEN_W) + x];
80
81 for (int32_t yy = 0; yy < h; yy++)
82 {
83 memcpy(dstPtr, srcPtr, w * sizeof (int32_t));
84
85 srcPtr += w;
86 dstPtr += SCREEN_W;
87 }
88 }
89
90 void putPixel(int32_t x, int32_t y, const uint32_t pixelColor)
91 {
92 video.frameBuffer[(y * SCREEN_W) + x] = pixelColor;
93 }
94
95 void hLine(int32_t x, int32_t y, int32_t w, const uint32_t pixelColor)
96 {
97 uint32_t *dstPtr = &video.frameBuffer[(y * SCREEN_W) + x];
98 for (int32_t xx = 0; xx < w; xx++)
99 dstPtr[xx] = pixelColor;
100 }
101
102 void vLine(int32_t x, int32_t y, int32_t h, const uint32_t pixelColor)
103 {
104 uint32_t *dstPtr = &video.frameBuffer[(y * SCREEN_W) + x];
105 for (int32_t yy = 0; yy < h; yy++)
106 {
107 *dstPtr = pixelColor;
108 dstPtr += SCREEN_W;
109 }
110 }
111
112 void drawFramework1(int32_t x, int32_t y, int32_t w, int32_t h)
113 {
114 hLine(x, y, w-1, video.palette[PAL_BORDER]);
115 vLine(x, y+1, h-2, video.palette[PAL_BORDER]);
116 hLine(x+1, y+h-1, w-1, video.palette[PAL_GENBKG2]);
117 vLine(x+w-1, y+1, h-2, video.palette[PAL_GENBKG2]);
118
119 putPixel(x, y+h-1, video.palette[PAL_GENBKG]);
120 putPixel(x+w-1, y, video.palette[PAL_GENBKG]);
121
122 fillRect(x+1, y+1, w-2, h-2, video.palette[PAL_GENBKG]);
123 }
124
125 void drawFramework2(int32_t x, int32_t y, int32_t w, int32_t h)
126 {
127 hLine(x, y, w-1, video.palette[PAL_GENBKG2]);
128 vLine(x, y+1, h-2, video.palette[PAL_GENBKG2]);
129 hLine(x+1, y+h-1, w-1, video.palette[PAL_BORDER]);
130 vLine(x+w-1, y+1, h-2, video.palette[PAL_BORDER]);
131
132 putPixel(x, y+h-1, video.palette[PAL_GENBKG]);
133 putPixel(x+w-1, y, video.palette[PAL_GENBKG]);
134
135 fillRect(x+1, y+1, w-2, h-2, video.palette[PAL_GENBKG]);
136 }
137
138 void drawFramework3(int32_t x, int32_t y, int32_t w, int32_t h)
139 {
140 fillRect(x, y, w, h, video.palette[PAL_GENBKG]);
141
142 vLine(x, y, h-1, video.palette[PAL_BORDER]);
143 vLine(x+1, y, h-2, video.palette[PAL_BORDER]);
144 hLine(x+2, y, w-3, video.palette[PAL_BORDER]);
145 hLine(x+2, y+1, w-4, video.palette[PAL_BORDER]);
146 hLine(x+1, y+h-1, w-1, video.palette[PAL_GENBKG2]);
147 hLine(x+2, y+h-2, w-2, video.palette[PAL_GENBKG2]);
148 vLine(x+w-2, y+2, h-4, video.palette[PAL_GENBKG2]);
149 vLine(x+w-1, y+1, h-3, video.palette[PAL_GENBKG2]);
150 }
151
152 void fillRect(int32_t x, int32_t y, int32_t w, int32_t h, const uint32_t pixelColor)
153 {
154 uint32_t *dstPtr = &video.frameBuffer[(y * SCREEN_W) + x];
155
156 for (int32_t yy = 0; yy < h; yy++)
157 {
158 for (int32_t xx = 0; xx < w; xx++)
159 dstPtr[xx] = pixelColor;
160
161 dstPtr += SCREEN_W;
162 }
163 }
164
165 void drawButton1(int32_t x, int32_t y, int32_t w, int32_t h, const char *text)
166 {
167 const int32_t textW = (int32_t)strlen(text) * (FONT_CHAR_W - 1);
168
169 drawFramework1(x, y, w, h);
170
171 int32_t textX = x + ((w - textW) / 2);
172 int32_t textY = y + ((h - FONT_CHAR_H) / 2);
173
174 // kludge
175 if (!strcmp(text, ARROW_UP_STR) || !strcmp(text, ARROW_DOWN_STR))
176 {
177 textX--;
178 textY--;
179 }
180
181 textOut2(textX, textY, text);
182 }
183
184 void drawButton2(int32_t x, int32_t y, int32_t w, int32_t h, const char *text)
185 {
186 const int32_t textW = (int32_t)strlen(text) * (FONT_CHAR_W - 1);
187
188 drawFramework2(x, y, w, h);
189
190 int32_t textX = x + ((w - textW) / 2);
191 int32_t textY = y + ((h - FONT_CHAR_H) / 2);
192
193 // kludge
194 if (!strcmp(text, ARROW_UP_STR) || !strcmp(text, ARROW_DOWN_STR))
195 {
196 textX--;
197 textY--;
198 }
199
200 textOut2(textX, textY, text);
201 }
202
203 void drawUpButton(int32_t x, int32_t y)
204 {
205 drawFramework1(x, y, 11, 11);
206 textOut2(x+1, y+2, ARROW_UP_STR);
207 }
208
209 void drawDownButton(int32_t x, int32_t y)
210 {
211 drawFramework1(x, y, 11, 11);
212 textOut2(x+1, y+2, ARROW_DOWN_STR);
213 }
214
215 void statusAllRight(void)
216 {
217 setStatusMessage("ALL RIGHT", DO_CARRY);
218 }
219
220 void statusOutOfMemory(void)
221 {
222 displayErrorMsg("OUT OF MEMORY !!!");
223 }
224
225 void statusSampleIsEmpty(void)
226 {
227 displayErrorMsg("SAMPLE IS EMPTY");
228 }
229
230 void changeStatusText(const char *text)
231 {
232 fillRect(88, 127, 17*FONT_CHAR_W, FONT_CHAR_H, video.palette[PAL_GENBKG]);
233 textOut(88, 127, text, video.palette[PAL_GENTXT]);
234 }
235
236 void statusNotSampleZero(void)
237 {
238 /* This rather confusing error message actually means that
239 ** you can't load a sample to sample slot #0 (which isn't a
240 ** real sample slot).
241 */
242 displayErrorMsg("NOT SAMPLE 0 !");
243 }
244
245 void renderFrame2(void) // use this in askBox()
246 {
247 updateMod2WavDialog(); // must be first to avoid flickering issues
248
249 updateSongInfo1(); // top left side of screen, when "disk op"/"pos ed" is hidden
250 updateSongInfo2(); // two middle rows of screen, always visible
251 updatePatternData();
252 updateSampler();
253 handleLastGUIObjectDown();
254 drawSamplerLine();
255 writeSampleMonitorWaveform();
256 }
257
258 void renderFrame(void)
259 {
260 renderFrame2();
261
262 updateEditOp();
263 updateDiskOp();
264 updatePosEd();
265 updateVisualizer();
266
267 // show [EDITING] in window title if in edit mode
268 if (oldCurrMode != editor.currMode)
269 {
270 oldCurrMode = editor.currMode;
271 if (song != NULL)
272 updateWindowTitle(song->modified);
273 }
274 }
275
276 void resetAllScreens(void) // prepare GUI for "really quit?" dialog
277 {
278 editor.mixFlag = false;
279 editor.swapChannelFlag = false;
280
281 ui.changingChordNote = false;
282 ui.changingSmpResample = false;
283 ui.changingSamplingNote = false;
284 ui.changingDrumPadNote = false;
285
286 ui.disableVisualizer = false;
287
288 if (ui.samplerScreenShown)
289 {
290 if (ui.samplingBoxShown)
291 {
292 ui.samplingBoxShown = false;
293 removeSamplingBox();
294 }
295
296 ui.samplerVolBoxShown = false;
297 ui.samplerFiltersBoxShown = false;
298 displaySample(); // removes volume/filter box
299 }
300
301 if (ui.editTextFlag)
302 leaveTextEditMode(EDIT_TEXT_NO_UPDATE);
303 }
304
305 static void fillFromVuMetersBgBuffer(void)
306 {
307 if (ui.samplerScreenShown || editor.mod2WavOngoing || editor.pat2SmpOngoing)
308 return;
309
310 const uint32_t *srcPtr = vuMetersBg;
311 uint32_t *dstPtr = &video.frameBuffer[(187 * SCREEN_W) + 55];
312
313 for (uint32_t i = 0; i < PAULA_VOICES; i++)
314 {
315 for (uint32_t y = 0; y < 48; y++)
316 {
317 for (uint32_t x = 0; x < 10; x++)
318 dstPtr[x] = srcPtr[x];
319
320 srcPtr += 10;
321 dstPtr -= SCREEN_W;
322 }
323
324 dstPtr += (SCREEN_W * 48) + 72;
325 }
326 }
327
328 void fillToVuMetersBgBuffer(void)
329 {
330 if (ui.samplerScreenShown || editor.mod2WavOngoing || editor.pat2SmpOngoing)
331 return;
332
333 const uint32_t *srcPtr = &video.frameBuffer[(187 * SCREEN_W) + 55];
334 uint32_t *dstPtr = vuMetersBg;
335
336 for (uint32_t i = 0; i < PAULA_VOICES; i++)
337 {
338 for (uint32_t y = 0; y < 48; y++)
339 {
340 for (uint32_t x = 0; x < 10; x++)
341 dstPtr[x] = srcPtr[x];
342
343 srcPtr -= SCREEN_W;
344 dstPtr += 10;
345 }
346
347 srcPtr += (SCREEN_W * 48) + 72;
348 }
349 }
350
351 void renderVuMeters(void)
352 {
353 if (ui.samplerScreenShown || editor.mod2WavOngoing || editor.pat2SmpOngoing)
354 return;
355
356 fillToVuMetersBgBuffer();
357
358 uint32_t *dstPtr = &video.frameBuffer[(187 * SCREEN_W) + 55];
359 for (uint32_t i = 0; i < PAULA_VOICES; i++)
360 {
361 uint32_t h;
362 if (config.realVuMeters)
363 h = editor.realVuMeterVolumes[i];
364 else
365 h = editor.vuMeterVolumes[i];
366
367 if (h > 48)
368 h = 48;
369
370 const uint32_t *srcPtr = vuMeterBMP;
371 for (uint32_t y = 0; y < h; y++)
372 {
373 for (uint32_t x = 0; x < 10; x++)
374 dstPtr[x] = srcPtr[x];
375
376 srcPtr += 10;
377 dstPtr -= SCREEN_W;
378 }
379
380 dstPtr += (SCREEN_W * h) + 72;
381 }
382 }
383
384 void updateSongInfo1(void) // left side of screen, when Disk Op. is hidden
385 {
386 if (ui.diskOpScreenShown)
387 return;
388
389 moduleSample_t *currSample = &song->samples[editor.currSample];
390
391 if (ui.updateSongPos)
392 {
393 ui.updateSongPos = false;
394 printThreeDecimalsBg(72, 3, *editor.currPosDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
395 }
396
397 if (ui.updateSongPattern)
398 {
399 ui.updateSongPattern = false;
400 printTwoDecimalsBg(80, 14, *editor.currPatternDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
401 }
402
403 if (ui.updateSongLength)
404 {
405 ui.updateSongLength = false;
406 if (!editor.mod2WavOngoing)
407 printThreeDecimalsBg(72, 25, *editor.currLengthDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
408 }
409
410 if (ui.updateCurrSampleFineTune)
411 {
412 ui.updateCurrSampleFineTune = false;
413
414 if (!editor.mod2WavOngoing)
415 textOutBg(80, 36, ftuneStrTab[currSample->fineTune & 0xF], video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
416 }
417
418 if (ui.updateCurrSampleNum)
419 {
420 ui.updateCurrSampleNum = false;
421 if (!editor.mod2WavOngoing)
422 {
423 printTwoHexBg(80, 47,
424 editor.sampleZero ? 0 : ((*editor.currSampleDisp) + 1), video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
425 }
426 }
427
428 if (ui.updateCurrSampleVolume)
429 {
430 ui.updateCurrSampleVolume = false;
431 if (!editor.mod2WavOngoing)
432 printTwoHexBg(80, 58, *currSample->volumeDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
433 }
434
435 if (ui.updateCurrSampleLength)
436 {
437 ui.updateCurrSampleLength = false;
438 if (!editor.mod2WavOngoing)
439 {
440 if (config.maxSampleLength == 0xFFFE)
441 printFourHexBg(64, 69, *currSample->lengthDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
442 else
443 printFiveHexBg(56, 69, *currSample->lengthDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
444 }
445 }
446
447 if (ui.updateCurrSampleRepeat)
448 {
449 ui.updateCurrSampleRepeat = false;
450 if (config.maxSampleLength == 0xFFFE)
451 printFourHexBg(64, 80, *currSample->loopStartDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
452 else
453 printFiveHexBg(56, 80, *currSample->loopStartDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
454 }
455
456 if (ui.updateCurrSampleReplen)
457 {
458 ui.updateCurrSampleReplen = false;
459 if (config.maxSampleLength == 0xFFFE)
460 printFourHexBg(64, 91, *currSample->loopLengthDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
461 else
462 printFiveHexBg(56, 91, *currSample->loopLengthDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
463 }
464 }
465
466 void updateSongInfo2(void) // two middle rows of screen, always present
467 {
468 if (ui.updateStatusText)
469 {
470 ui.updateStatusText = false;
471
472 // clear background
473 fillRect(88, 127, 17*FONT_CHAR_W, FONT_CHAR_H, video.palette[PAL_GENBKG]);
474
475 // render status text
476 if (!editor.errorMsgActive && editor.blockMarkFlag && !ui.askBoxShown && !editor.swapChannelFlag)
477 {
478 textOut(88, 127, "MARK BLOCK", video.palette[PAL_GENTXT]);
479 charOut(192, 127, '-', video.palette[PAL_GENTXT]);
480
481 editor.blockToPos = song->currRow;
482 if (editor.blockFromPos >= editor.blockToPos)
483 {
484 printTwoDecimals(176, 127, editor.blockToPos, video.palette[PAL_GENTXT]);
485 printTwoDecimals(200, 127, editor.blockFromPos, video.palette[PAL_GENTXT]);
486 }
487 else
488 {
489 printTwoDecimals(176, 127, editor.blockFromPos, video.palette[PAL_GENTXT]);
490 printTwoDecimals(200, 127, editor.blockToPos, video.palette[PAL_GENTXT]);
491 }
492 }
493 else
494 {
495 textOut(88, 127, ui.statusMessage, video.palette[PAL_GENTXT]);
496 }
497 }
498
499 if (ui.updateSongBPM)
500 {
501 ui.updateSongBPM = false;
502 if (!ui.samplerScreenShown)
503 printThreeDecimalsBg(32, 123, song->currBPM, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
504 }
505
506 if (ui.updateCurrPattText)
507 {
508 ui.updateCurrPattText = false;
509 if (!ui.samplerScreenShown)
510 printTwoDecimalsBg(8, 127, *editor.currEditPatternDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
511 }
512
513 if (ui.updateTrackerFlags)
514 {
515 ui.updateTrackerFlags = false;
516
517 charOutBg(1, 113, ' ', video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
518 charOutBg(8, 113, ' ', video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
519
520 if (editor.autoInsFlag)
521 {
522 charOut(0, 113, 'I', video.palette[PAL_GENTXT]);
523
524 // in Amiga PT, "auto insert" 9 means 0
525 if (editor.autoInsSlot == 9)
526 charOut(8, 113, '0', video.palette[PAL_GENTXT]);
527 else
528 charOut(8, 113, '1' + editor.autoInsSlot, video.palette[PAL_GENTXT]);
529 }
530
531 charOutBg(1, 102, ' ', video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
532 if (editor.metroFlag)
533 charOut(0, 102, 'M', video.palette[PAL_GENTXT]);
534
535 charOutBg(16, 102, ' ', video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
536 if (editor.multiFlag)
537 charOut(16, 102, 'M', video.palette[PAL_GENTXT]);
538
539 charOutBg(24, 102, '0' + editor.editMoveAdd, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
540
541 charOutBg(311, 128, ' ', video.palette[PAL_GENBKG], video.palette[PAL_GENBKG]);
542 if (editor.pNoteFlag == 1)
543 {
544 putPixel(314, 129, video.palette[PAL_GENTXT]);
545 putPixel(315, 129, video.palette[PAL_GENTXT]);
546 }
547 else if (editor.pNoteFlag == 2)
548 {
549 putPixel(314, 128, video.palette[PAL_GENTXT]);
550 putPixel(315, 128, video.palette[PAL_GENTXT]);
551 putPixel(314, 130, video.palette[PAL_GENTXT]);
552 putPixel(315, 130, video.palette[PAL_GENTXT]);
553 }
554 }
555
556 // playback timer
557
558 uint32_t seconds = editor.playbackSeconds;
559 if (seconds <= 5999) // below 100 minutes (99:59 is max for the UI)
560 {
561 const uint32_t MI_TimeM = seconds / 60;
562 const uint32_t MI_TimeS = seconds - (MI_TimeM * 60);
563
564 // xx:xx
565 printTwoDecimalsBg(272, 102, MI_TimeM, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
566 printTwoDecimalsBg(296, 102, MI_TimeS, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
567 }
568 else
569 {
570 // 99:59
571 printTwoDecimalsBg(272, 102, 99, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
572 printTwoDecimalsBg(296, 102, 59, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
573 }
574
575 if (ui.updateSongName)
576 {
577 ui.updateSongName = false;
578 for (int32_t x = 0; x < 20; x++)
579 {
580 char tempChar = song->header.name[x];
581 if (tempChar == '\0')
582 tempChar = '_';
583
584 charOutBg(104 + (x * FONT_CHAR_W), 102, tempChar, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
585 }
586 }
587
588 if (ui.updateCurrSampleName)
589 {
590 ui.updateCurrSampleName = false;
591 moduleSample_t *currSample = &song->samples[editor.currSample];
592
593 for (int32_t x = 0; x < 22; x++)
594 {
595 char tempChar = currSample->text[x];
596 if (tempChar == '\0')
597 tempChar = '_';
598
599 charOutBg(104 + (x * FONT_CHAR_W), 113, tempChar, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
600 }
601 }
602
603 if (ui.updateSongSize)
604 {
605 ui.updateSongSize = false;
606
607 // clear background
608 fillRect(264, 123, 6*FONT_CHAR_W, FONT_CHAR_H, video.palette[PAL_GENBKG]);
609
610 // calculate module length
611
612 uint32_t totalSampleDataSize = 0;
613 for (int32_t i = 0; i < MOD_SAMPLES; i++)
614 totalSampleDataSize += song->samples[i].length;
615
616 uint32_t totalPatterns = 0;
617 for (int32_t i = 0; i < 128; i++)
618 {
619 if (song->header.patternTable[i] > totalPatterns)
620 totalPatterns = song->header.patternTable[i];
621 }
622
623 uint32_t moduleSize = 2108 + (totalPatterns * 1024) + totalSampleDataSize;
624 if (moduleSize > 999999)
625 {
626 charOut(304, 123, 'K', video.palette[PAL_GENTXT]);
627 printFourDecimals(272, 123, moduleSize / 1000, video.palette[PAL_GENTXT]);
628 }
629 else
630 {
631 printSixDecimals(264, 123, moduleSize, video.palette[PAL_GENTXT]);
632 }
633 }
634
635 if (ui.updateSongTiming)
636 {
637 ui.updateSongTiming = false;
638 textOutBg(288, 130, (editor.timingMode == TEMPO_MODE_CIA) ? "CIA" : "VBL", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
639 }
640 }
641
642 void updateCursorPos(void)
643 {
644 if (!ui.samplerScreenShown)
645 setSpritePos(SPRITE_PATTERN_CURSOR, cursorPosTable[cursor.pos], 188);
646 }
647
648 void updateSampler(void)
649 {
650 if (!ui.samplerScreenShown || ui.samplingBoxShown)
651 return;
652
653 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
654 moduleSample_t *s = &song->samples[editor.currSample];
655
656 // update 9xx offset
657 if (mouse.y >= 138 && mouse.y <= 201 && mouse.x >= 3 && mouse.x <= 316)
658 {
659 if (!ui.samplerVolBoxShown && !ui.samplerFiltersBoxShown && s->length > 0)
660 {
661 int32_t tmpSampleOffset = 0x900 + (scr2SmpPos(mouse.x-3) >> 8);
662 if (tmpSampleOffset != ui.lastSampleOffset)
663 {
664 ui.lastSampleOffset = (uint16_t)tmpSampleOffset;
665 ui.update9xxPos = true;
666 }
667 }
668 }
669
670 // display 9xx offset
671 if (ui.update9xxPos)
672 {
673 ui.update9xxPos = false;
674 if (ui.lastSampleOffset <= 0x900 || ui.lastSampleOffset > 0x9FF)
675 textOutBg(288, 247, "---", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
676 else
677 printThreeHexBg(288, 247, ui.lastSampleOffset, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
678 }
679
680 if (ui.updateResampleNote)
681 {
682 ui.updateResampleNote = false;
683
684 // show resample note
685 if (ui.changingSmpResample)
686 {
687 textOutBg(288, 236, "---", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
688 }
689 else
690 {
691 ASSERT(editor.resampleNote < 36);
692 textOutBg(288, 236,
693 config.accidental ? noteNames2[2+editor.resampleNote] : noteNames1[2+editor.resampleNote],
694 video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
695 }
696 }
697
698 if (ui.samplerVolBoxShown)
699 {
700 if (ui.updateVolFromText)
701 {
702 ui.updateVolFromText = false;
703 printThreeDecimalsBg(176, 157, *editor.vol1Disp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
704 }
705
706 if (ui.updateVolToText)
707 {
708 ui.updateVolToText = false;
709 printThreeDecimalsBg(176, 168, *editor.vol2Disp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
710 }
711 }
712 else if (ui.samplerFiltersBoxShown)
713 {
714 if (ui.updateLPText)
715 {
716 ui.updateLPText = false;
717 printFourDecimalsBg(168, 157, *editor.lpCutOffDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
718 }
719
720 if (ui.updateHPText)
721 {
722 ui.updateHPText = false;
723 printFourDecimalsBg(168, 168, *editor.hpCutOffDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
724 }
725
726 if (ui.updateNormFlag)
727 {
728 ui.updateNormFlag = false;
729
730 if (editor.normalizeFiltersFlag)
731 textOutBg(208, 179, "YES", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
732 else
733 textOutBg(208, 179, "NO ", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
734 }
735 }
736 }
737
738 void showVolFromSlider(void)
739 {
740 uint32_t sliderStart = ((editor.vol1 * 3) + 5) / 10;
741 uint32_t sliderEnd = sliderStart + 4;
742 uint32_t pixel = video.palette[PAL_QADSCP];
743 uint32_t bgPixel = video.palette[PAL_BACKGRD];
744 uint32_t *dstPtr = &video.frameBuffer[(158 * SCREEN_W) + 105];
745
746 for (uint32_t y = 0; y < 3; y++)
747 {
748 for (uint32_t x = 0; x < 65; x++)
749 {
750 if (x >= sliderStart && x <= sliderEnd)
751 dstPtr[x] = pixel;
752 else
753 dstPtr[x] = bgPixel;
754 }
755
756 dstPtr += SCREEN_W;
757 }
758 }
759
760 void showVolToSlider(void)
761 {
762 uint32_t sliderStart = ((editor.vol2 * 3) + 5) / 10;
763 uint32_t sliderEnd = sliderStart + 4;
764 uint32_t pixel = video.palette[PAL_QADSCP];
765 uint32_t bgPixel = video.palette[PAL_BACKGRD];
766 uint32_t *dstPtr = &video.frameBuffer[(169 * SCREEN_W) + 105];
767
768 for (uint32_t y = 0; y < 3; y++)
769 {
770 for (uint32_t x = 0; x < 65; x++)
771 {
772 if (x >= sliderStart && x <= sliderEnd)
773 dstPtr[x] = pixel;
774 else
775 dstPtr[x] = bgPixel;
776 }
777
778 dstPtr += SCREEN_W;
779 }
780 }
781
782 void renderSamplerVolBox(void)
783 {
784 blit32(72, 154, 136, 33, samplerVolumeBMP);
785
786 ui.updateVolFromText = true;
787 ui.updateVolToText = true;
788 showVolFromSlider();
789 showVolToSlider();
790
791 // hide loop sprites
792 hideSprite(SPRITE_LOOP_PIN_LEFT);
793 hideSprite(SPRITE_LOOP_PIN_RIGHT);
794 }
795
796 void removeSamplerVolBox(void)
797 {
798 displaySample();
799 }
800
801 void renderSamplerFiltersBox(void)
802 {
803 blit32(65, 154, 186, 33, samplerFiltersBMP);
804
805 textOut(200, 157, "HZ", video.palette[PAL_GENTXT]);
806 textOut(200, 168, "HZ", video.palette[PAL_GENTXT]);
807
808 ui.updateLPText = true;
809 ui.updateHPText = true;
810 ui.updateNormFlag = true;
811
812 // hide loop sprites
813 hideSprite(SPRITE_LOOP_PIN_LEFT);
814 hideSprite(SPRITE_LOOP_PIN_RIGHT);
815 }
816
817 void removeSamplerFiltersBox(void)
818 {
819 displaySample();
820 }
821
822 void renderMuteButtons(void)
823 {
824 if (ui.diskOpScreenShown || ui.posEdScreenShown)
825 return;
826
827 uint32_t *dstPtr = &video.frameBuffer[(3 * SCREEN_W) + 310];
828 for (uint32_t i = 0; i < PAULA_VOICES; i++)
829 {
830 const uint32_t *srcPtr;
831 uint32_t srcPitch;
832
833 if (editor.muted[i])
834 {
835 srcPtr = &muteButtonsBMP[i * (6 * 7)];
836 srcPitch = 7;
837 }
838 else
839 {
840 srcPtr = &trackerFrameBMP[((3 + (i * 11)) * SCREEN_W) + 310];
841 srcPitch = SCREEN_W;
842 }
843
844 for (uint32_t y = 0; y < 6; y++)
845 {
846 for (uint32_t x = 0; x < 7; x++)
847 dstPtr[x] = srcPtr[x];
848
849 srcPtr += srcPitch;
850 dstPtr += SCREEN_W;
851 }
852
853 dstPtr += SCREEN_W * 5;
854 }
855 }
856
857 void updateCurrSample(void)
858 {
859 ui.updateCurrSampleName = true;
860 ui.updateSongSize = true;
861
862 if (!ui.diskOpScreenShown)
863 {
864 ui.updateCurrSampleFineTune = true;
865 ui.updateCurrSampleNum = true;
866 ui.updateCurrSampleVolume = true;
867 ui.updateCurrSampleLength = true;
868 ui.updateCurrSampleRepeat = true;
869 ui.updateCurrSampleReplen = true;
870 }
871
872 if (ui.samplerScreenShown)
873 redrawSample();
874
875 updateSamplePos();
876 recalcChordLength();
877
878 sampler.tmpLoopStart = 0;
879 sampler.tmpLoopLength = 0;
880 }
881
882 void updatePatternData(void)
883 {
884 if (ui.updatePatternData)
885 {
886 ui.updatePatternData = false;
887 if (!ui.samplerScreenShown)
888 redrawPattern();
889 }
890 }
891
892 static void sendMouseButtonUpEvent(uint8_t button)
893 {
894 SDL_Event event;
895
896 memset(&event, 0, sizeof (event));
897
898 event.type = SDL_MOUSEBUTTONUP;
899 event.button.button = button;
900
901 SDL_PushEvent(&event);
902 }
903
904 void handleLastGUIObjectDown(void)
905 {
906 bool testMouseButtonRelease = false;
907
908 if (ui.sampleMarkingPos >= 0)
909 {
910 samplerSamplePressed(MOUSE_BUTTON_HELD);
911 testMouseButtonRelease = true;
912 }
913
914 if (ui.forceSampleDrag)
915 {
916 samplerBarPressed(MOUSE_BUTTON_HELD);
917 testMouseButtonRelease = true;
918 }
919
920 if (ui.forceSampleEdit)
921 {
922 samplerEditSample(MOUSE_BUTTON_HELD);
923 testMouseButtonRelease = true;
924 }
925
926 if (ui.forceVolDrag)
927 {
928 volBoxBarPressed(MOUSE_BUTTON_HELD);
929 testMouseButtonRelease = true;
930 }
931
932 /* Hack to send "mouse button up" events if we released the mouse button(s)
933 ** outside of the window...
934 */
935 if (testMouseButtonRelease)
936 {
937 if (mouse.x < 0 || mouse.x >= SCREEN_W || mouse.y < 0 || mouse.y >= SCREEN_H)
938 {
939 if (mouse.leftButtonPressed && !(mouse.buttonState & SDL_BUTTON_LMASK))
940 sendMouseButtonUpEvent(SDL_BUTTON_LEFT);
941
942 if (mouse.rightButtonPressed && !(mouse.buttonState & SDL_BUTTON_RMASK))
943 sendMouseButtonUpEvent(SDL_BUTTON_RIGHT);
944 }
945 }
946 }
947
948 void updateVisualizer(void)
949 {
950 if (ui.disableVisualizer || ui.diskOpScreenShown ||
951 ui.posEdScreenShown || ui.editOpScreenShown ||
952 ui.aboutScreenShown || ui.askBoxShown ||
953 editor.mod2WavOngoing || ui.samplingBoxShown)
954 {
955 return;
956 }
957
958 if (ui.visualizerMode == VISUAL_SPECTRUM)
959 {
960 // spectrum analyzer
961
962 uint32_t *dstPtr = &video.frameBuffer[(59 * SCREEN_W) + 129];
963 for (uint32_t i = 0; i < SPECTRUM_BAR_NUM; i++)
964 {
965 const uint32_t *srcPtr = analyzerColorsRGB24;
966 uint32_t pixel = video.palette[PAL_GENBKG];
967
968 int32_t tmpVol = editor.spectrumVolumes[i];
969 if (tmpVol > SPECTRUM_BAR_HEIGHT)
970 tmpVol = SPECTRUM_BAR_HEIGHT;
971
972 for (int32_t y = SPECTRUM_BAR_HEIGHT-1; y >= 0; y--)
973 {
974 if (y < tmpVol)
975 pixel = srcPtr[y];
976
977 for (uint32_t x = 0; x < SPECTRUM_BAR_WIDTH; x++)
978 dstPtr[x] = pixel;
979
980 dstPtr += SCREEN_W;
981 }
982
983 dstPtr -= (SCREEN_W * SPECTRUM_BAR_HEIGHT) - (SPECTRUM_BAR_WIDTH + 2);
984 }
985 }
986 else
987 {
988 drawScopes();
989 }
990 }
991
992 void renderQuadrascopeBg(void)
993 {
994 const uint32_t *srcPtr = &trackerFrameBMP[(44 * SCREEN_W) + 120];
995 uint32_t *dstPtr = &video.frameBuffer[(44 * SCREEN_W) + 120];
996
997 for (uint32_t y = 0; y < 55; y++)
998 {
999 memcpy(dstPtr, srcPtr, 200 * sizeof (int32_t));
1000
1001 srcPtr += SCREEN_W;
1002 dstPtr += SCREEN_W;
1003 }
1004 }
1005
1006 void renderSpectrumAnalyzerBg(void)
1007 {
1008 blit32(120, 44, 200, 55, spectrumVisualsBMP);
1009 }
1010
1011 void renderAboutScreen(void)
1012 {
1013 char verString[16];
1014
1015 if (!ui.aboutScreenShown || ui.diskOpScreenShown || ui.posEdScreenShown || ui.editOpScreenShown)
1016 return;
1017
1018 blit32(120, 44, 200, 55, aboutScreenBMP);
1019
1020 // draw version string
1021
1022 sprintf(verString, "v%s", PROG_VER_STR);
1023 uint32_t verStringX = 260 + (((63 - ((uint32_t)strlen(verString) * (FONT_CHAR_W - 1))) + 1) / 2);
1024 textOutTight(verStringX, 67, verString, video.palette[PAL_GENBKG2]);
1025 }
1026
1027 void renderEditOpMode(void)
1028 {
1029 const uint32_t *srcPtr;
1030
1031 // select what character box to render
1032 switch (ui.editOpScreen)
1033 {
1034 default:
1035 case 0:
1036 srcPtr = &editOpModeCharsBMP[editor.sampleAllFlag ? EDOP_MODE_BMP_A_OFS : EDOP_MODE_BMP_S_OFS];
1037 break;
1038
1039 case 1:
1040 {
1041 if (editor.trackPattFlag == 0)
1042 srcPtr = &editOpModeCharsBMP[EDOP_MODE_BMP_T_OFS];
1043 else if (editor.trackPattFlag == 1)
1044 srcPtr = &editOpModeCharsBMP[EDOP_MODE_BMP_P_OFS];
1045 else
1046 srcPtr = &editOpModeCharsBMP[EDOP_MODE_BMP_S_OFS];
1047 }
1048 break;
1049
1050 case 2:
1051 srcPtr = &editOpModeCharsBMP[editor.halveSampleFlag ? EDOP_MODE_BMP_H_OFS : EDOP_MODE_BMP_C_OFS];
1052 break;
1053
1054 case 3:
1055 srcPtr = (editor.newOldFlag == 0) ? &editOpModeCharsBMP[EDOP_MODE_BMP_N_OFS] : &editOpModeCharsBMP[EDOP_MODE_BMP_O_OFS];
1056 break;
1057 }
1058
1059 blit32(310, 47, 7, 6, srcPtr);
1060 }
1061
1062 void renderEditOpScreen(void)
1063 {
1064 const uint32_t *srcPtr;
1065
1066 // select which graphics to render
1067 switch (ui.editOpScreen)
1068 {
1069 default:
1070 case 0: srcPtr = editOpScreen1BMP; break;
1071 case 1: srcPtr = editOpScreen2BMP; break;
1072 case 2: srcPtr = editOpScreen3BMP; break;
1073 case 3: srcPtr = editOpScreen4BMP; break;
1074 }
1075
1076 blit32(120, 44, 200, 55, srcPtr);
1077
1078 // fix graphics in 128K sample mode
1079 if (config.maxSampleLength != 65534)
1080 {
1081 if (ui.editOpScreen == 2)
1082 blit32(213, 55, 32, 11, fix128KPosBMP);
1083 else if (ui.editOpScreen == 3)
1084 blit32(120, 88, 48, 11, fix128KChordBMP);
1085 }
1086
1087 renderEditOpMode();
1088
1089 // render text and content
1090 if (ui.editOpScreen == 0)
1091 {
1092 textOut(128, 47, " TRACK PATTERN ", video.palette[PAL_GENTXT]);
1093 }
1094 else if (ui.editOpScreen == 1)
1095 {
1096 textOut(128, 47, " RECORD SAMPLES ", video.palette[PAL_GENTXT]);
1097
1098 ui.updateRecordText = true;
1099 ui.updateQuantizeText = true;
1100 ui.updateMetro1Text = true;
1101 ui.updateMetro2Text = true;
1102 ui.updateFromText = true;
1103 ui.updateKeysText = true;
1104 ui.updateToText = true;
1105 }
1106 else if (ui.editOpScreen == 2)
1107 {
1108 textOut(128, 47, " SAMPLE EDITOR ", video.palette[PAL_GENTXT]);
1109 charOut(272, 91, '%', video.palette[PAL_GENTXT]); // for Volume text
1110
1111 ui.updatePosText = true;
1112 ui.updateModText = true;
1113 ui.updateVolText = true;
1114 }
1115 else if (ui.editOpScreen == 3)
1116 {
1117 textOut(128, 47, " SAMPLE CHORD EDITOR ", video.palette[PAL_GENTXT]);
1118
1119 ui.updateChordLengthText = true;
1120 ui.updateChordNote1Text = true;
1121 ui.updateChordNote2Text = true;
1122 ui.updateChordNote3Text = true;
1123 ui.updateChordNote4Text = true;
1124 }
1125 }
1126
1127 void updateEditOp(void)
1128 {
1129 if (!ui.editOpScreenShown || ui.posEdScreenShown || ui.diskOpScreenShown)
1130 return;
1131
1132 if (ui.editOpScreen == 1)
1133 {
1134 if (ui.updateRecordText)
1135 {
1136 ui.updateRecordText = false;
1137 textOutBg(176, 58, (editor.recordMode == RECORD_PATT) ? "PATT" : "SONG", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1138 }
1139
1140 if (ui.updateQuantizeText)
1141 {
1142 ui.updateQuantizeText = false;
1143 printTwoDecimalsBg(192, 69, *editor.quantizeValueDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1144 }
1145
1146 if (ui.updateMetro1Text)
1147 {
1148 ui.updateMetro1Text = false;
1149 printTwoDecimalsBg(168, 80, *editor.metroSpeedDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1150 }
1151
1152 if (ui.updateMetro2Text)
1153 {
1154 ui.updateMetro2Text = false;
1155 printTwoDecimalsBg(192, 80, *editor.metroChannelDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1156 }
1157
1158 if (ui.updateFromText)
1159 {
1160 ui.updateFromText = false;
1161 printTwoHexBg(264, 80, *editor.sampleFromDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1162 }
1163
1164 if (ui.updateKeysText)
1165 {
1166 ui.updateKeysText = false;
1167 textOutBg(160, 91, editor.multiFlag ? "MULTI " : "SINGLE", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1168 }
1169
1170 if (ui.updateToText)
1171 {
1172 ui.updateToText = false;
1173 printTwoHexBg(264, 91, *editor.sampleToDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1174 }
1175 }
1176 else if (ui.editOpScreen == 2)
1177 {
1178 if (ui.updateMixText)
1179 {
1180 ui.updateMixText = false;
1181 if (editor.mixFlag)
1182 {
1183 textOutBg(128, 47, editor.mixText, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1184 textOutBg(248, 47, " ", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1185 }
1186 else
1187 {
1188 textOutBg(128, 47, " SAMPLE EDITOR ", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1189 }
1190 }
1191
1192 if (ui.updatePosText)
1193 {
1194 ui.updatePosText = false;
1195 if (config.maxSampleLength == 0xFFFE)
1196 printFourHexBg(248, 58, *editor.samplePosDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1197 else
1198 printFiveHexBg(240, 58, *editor.samplePosDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1199 }
1200
1201 if (ui.updateModText)
1202 {
1203 ui.updateModText = false;
1204 printThreeDecimalsBg(256, 69,
1205 (editor.modulateSpeed < 0) ? (0 - editor.modulateSpeed) : editor.modulateSpeed,
1206 video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1207
1208 if (editor.modulateSpeed < 0)
1209 charOutBg(248, 69, '-', video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1210 else
1211 charOutBg(248, 69, ' ', video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1212 }
1213
1214 if (ui.updateVolText)
1215 {
1216 ui.updateVolText = false;
1217 printThreeDecimalsBg(248, 91, *editor.sampleVolDisp, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1218 }
1219 }
1220 else if (ui.editOpScreen == 3)
1221 {
1222 if (ui.updateChordLengthText)
1223 {
1224 ui.updateChordLengthText = false;
1225
1226 // clear background
1227 if (config.maxSampleLength != 65534)
1228 textOutBg(160, 91, " ", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1229 else
1230 textOutBg(168, 91, " ", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1231
1232 charOut(198, 91, ':', video.palette[PAL_GENBKG]);
1233
1234 if (song->samples[editor.currSample].loopLength > 2 || song->samples[editor.currSample].loopStart >= 2)
1235 {
1236 textOut(168, 91, "LOOP", video.palette[PAL_GENTXT]);
1237 }
1238 else
1239 {
1240 if (config.maxSampleLength == 0xFFFE)
1241 printFourHex(168, 91, *editor.chordLengthDisp, video.palette[PAL_GENTXT]); // chord max length
1242 else
1243 printFiveHex(160, 91, *editor.chordLengthDisp, video.palette[PAL_GENTXT]); // chord max length
1244
1245 charOut(198, 91, (editor.chordLengthMin) ? '.' : ':', video.palette[PAL_GENTXT]); // min/max flag
1246 }
1247 }
1248
1249 if (ui.updateChordNote1Text)
1250 {
1251 ui.updateChordNote1Text = false;
1252 if (editor.note1 > 35)
1253 textOutBg(256, 58, "---", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1254 else
1255 textOutBg(256, 58, config.accidental ? noteNames2[2+editor.note1] : noteNames1[2+editor.note1],
1256 video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1257 }
1258
1259 if (ui.updateChordNote2Text)
1260 {
1261 ui.updateChordNote2Text = false;
1262 if (editor.note2 > 35)
1263 textOutBg(256, 69, "---", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1264 else
1265 textOutBg(256, 69, config.accidental ? noteNames2[2+editor.note2] : noteNames1[2+editor.note2],
1266 video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1267 }
1268
1269 if (ui.updateChordNote3Text)
1270 {
1271 ui.updateChordNote3Text = false;
1272 if (editor.note3 > 35)
1273 textOutBg(256, 80, "---", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1274 else
1275 textOutBg(256, 80, config.accidental ? noteNames2[2+editor.note3] : noteNames1[2+editor.note3],
1276 video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1277 }
1278
1279 if (ui.updateChordNote4Text)
1280 {
1281 ui.updateChordNote4Text = false;
1282 if (editor.note4 > 35)
1283 textOutBg(256, 91, "---", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1284 else
1285 textOutBg(256, 91, config.accidental ? noteNames2[2+editor.note4] : noteNames1[2+editor.note4],
1286 video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1287 }
1288 }
1289 }
1290
1291 void displayMainScreen(void)
1292 {
1293 editor.blockMarkFlag = false;
1294
1295 ui.updateSongName = true;
1296 ui.updateSongSize = true;
1297 ui.updateSongTiming = true;
1298 ui.updateTrackerFlags = true;
1299 ui.updateStatusText = true;
1300
1301 ui.updateCurrSampleName = true;
1302
1303 if (!ui.diskOpScreenShown)
1304 {
1305 ui.updateCurrSampleFineTune = true;
1306 ui.updateCurrSampleNum = true;
1307 ui.updateCurrSampleVolume = true;
1308 ui.updateCurrSampleLength = true;
1309 ui.updateCurrSampleRepeat = true;
1310 ui.updateCurrSampleReplen = true;
1311 }
1312
1313 if (ui.samplerScreenShown)
1314 {
1315 if (!ui.diskOpScreenShown)
1316 {
1317 blit32(0, 0, 320, 121, trackerFrameBMP);
1318
1319 if (config.maxSampleLength != 65534)
1320 blit32(1, 65, 62, 34, fix128KTrackerBMP); // fix for 128kB support mode
1321 }
1322 }
1323 else
1324 {
1325 if (!ui.diskOpScreenShown)
1326 {
1327 blit32(0, 0, 320, 255, trackerFrameBMP);
1328
1329 if (config.maxSampleLength != 65534)
1330 blit32(1, 65, 62, 34, fix128KTrackerBMP); // fix for 128kB support mode
1331 }
1332 else
1333 {
1334 blit32(0, 121, 320, 134, &trackerFrameBMP[121 * SCREEN_W]);
1335 }
1336
1337 ui.updateSongBPM = true;
1338 ui.updateCurrPattText = true;
1339 ui.updatePatternData = true;
1340 }
1341
1342 if (ui.diskOpScreenShown)
1343 {
1344 renderDiskOpScreen();
1345 }
1346 else
1347 {
1348 ui.updateSongPos = true;
1349 ui.updateSongPattern = true;
1350 ui.updateSongLength = true;
1351
1352 // draw zeroes that will never change (to the left of numbers)
1353
1354 charOut(64, 3, '0', video.palette[PAL_GENTXT]);
1355 textOut(64, 14, "00", video.palette[PAL_GENTXT]);
1356
1357 if (!editor.mod2WavOngoing)
1358 {
1359 charOut(64, 25, '0', video.palette[PAL_GENTXT]);
1360 textOut(64, 47, "00", video.palette[PAL_GENTXT]);
1361 textOut(64, 58, "00", video.palette[PAL_GENTXT]);
1362 }
1363
1364 if (ui.posEdScreenShown)
1365 {
1366 renderPosEdScreen();
1367 ui.updatePosEd = true;
1368 }
1369 else
1370 {
1371 if (ui.editOpScreenShown)
1372 {
1373 renderEditOpScreen();
1374 }
1375 else
1376 {
1377 if (ui.aboutScreenShown)
1378 {
1379 renderAboutScreen();
1380 }
1381 else
1382 {
1383 if (ui.visualizerMode == VISUAL_QUADRASCOPE) renderQuadrascopeBg();
1384 else if (ui.visualizerMode == VISUAL_SPECTRUM) renderSpectrumAnalyzerBg();
1385 }
1386 }
1387
1388 renderMuteButtons();
1389 }
1390 }
1391 }
1392
1393 void videoClose(void)
1394 {
1395 if (video.texture != NULL) SDL_DestroyTexture(video.texture);
1396 if (video.renderer != NULL) SDL_DestroyRenderer(video.renderer);
1397 if (video.window != NULL) SDL_DestroyWindow(video.window);
1398 if (video.frameBuffer != NULL) free(video.frameBuffer);
1399 }
1400
1401 void setupSprites(void)
1402 {
1403 memset(sprites, 0, sizeof (sprites));
1404
1405 sprites[SPRITE_MOUSE_POINTER].data = mousePointerBMP;
1406 sprites[SPRITE_MOUSE_POINTER].pixelType = SPRITE_TYPE_PALETTE;
1407 sprites[SPRITE_MOUSE_POINTER].colorKey = PAL_COLORKEY;
1408 sprites[SPRITE_MOUSE_POINTER].w = 16;
1409 sprites[SPRITE_MOUSE_POINTER].h = 16;
1410 hideSprite(SPRITE_MOUSE_POINTER);
1411
1412 sprites[SPRITE_PATTERN_CURSOR].data = patternCursorBMP;
1413 sprites[SPRITE_PATTERN_CURSOR].pixelType = SPRITE_TYPE_RGB;
1414 sprites[SPRITE_PATTERN_CURSOR].colorKey = video.palette[PAL_COLORKEY];
1415 sprites[SPRITE_PATTERN_CURSOR].w = 11;
1416 sprites[SPRITE_PATTERN_CURSOR].h = 14;
1417 hideSprite(SPRITE_PATTERN_CURSOR);
1418
1419 sprites[SPRITE_LOOP_PIN_LEFT].data = loopPinsBMP;
1420 sprites[SPRITE_LOOP_PIN_LEFT].pixelType = SPRITE_TYPE_RGB;
1421 sprites[SPRITE_LOOP_PIN_LEFT].colorKey = video.palette[PAL_COLORKEY];
1422 sprites[SPRITE_LOOP_PIN_LEFT].w = 4;
1423 sprites[SPRITE_LOOP_PIN_LEFT].h = 64;
1424 hideSprite(SPRITE_LOOP_PIN_LEFT);
1425
1426 sprites[SPRITE_LOOP_PIN_RIGHT].data = &loopPinsBMP[4 * 64];
1427 sprites[SPRITE_LOOP_PIN_RIGHT].pixelType = SPRITE_TYPE_RGB;
1428 sprites[SPRITE_LOOP_PIN_RIGHT].colorKey = video.palette[PAL_COLORKEY];
1429 sprites[SPRITE_LOOP_PIN_RIGHT].w = 4;
1430 sprites[SPRITE_LOOP_PIN_RIGHT].h = 64;
1431 hideSprite(SPRITE_LOOP_PIN_RIGHT);
1432
1433 sprites[SPRITE_SAMPLING_POS_LINE].data = samplingPosBMP;
1434 sprites[SPRITE_SAMPLING_POS_LINE].pixelType = SPRITE_TYPE_RGB;
1435 sprites[SPRITE_SAMPLING_POS_LINE].colorKey = video.palette[PAL_COLORKEY];
1436 sprites[SPRITE_SAMPLING_POS_LINE].w = 1;
1437 sprites[SPRITE_SAMPLING_POS_LINE].h = 64;
1438 hideSprite(SPRITE_SAMPLING_POS_LINE);
1439
1440 // setup refresh buffer (used to clear sprites after each frame)
1441 for (uint32_t i = 0; i < SPRITE_NUM; i++)
1442 sprites[i].refreshBuffer = (uint32_t *)malloc((sprites[i].w * sprites[i].h) * sizeof (int32_t));
1443 }
1444
1445 void freeSprites(void)
1446 {
1447 for (int32_t i = 0; i < SPRITE_NUM; i++)
1448 free(sprites[i].refreshBuffer);
1449 }
1450
1451 void setSpritePos(int32_t sprite, int32_t x, int32_t y)
1452 {
1453 sprites[sprite].newX = (int16_t)x;
1454 sprites[sprite].newY = (int16_t)y;
1455 }
1456
1457 void hideSprite(int32_t sprite)
1458 {
1459 sprites[sprite].newX = SCREEN_W;
1460 }
1461
1462 void eraseSprites(void)
1463 {
1464 for (int32_t i = SPRITE_NUM-1; i >= 0; i--) // erasing must be done in reverse order
1465 {
1466 sprite_t *s = &sprites[i];
1467 if (s->x >= SCREEN_W || s->y >= SCREEN_H) // sprite is hidden, don't draw nor fill clear buffer
1468 continue;
1469
1470 ASSERT(s->refreshBuffer != NULL);
1471
1472 int32_t sw = s->w;
1473 int32_t sh = s->h;
1474 int32_t sx = s->x;
1475 int32_t sy = s->y;
1476
1477 // if x is negative, adjust variables
1478 if (sx < 0)
1479 {
1480 sw += sx; // subtraction
1481 sx = 0;
1482 }
1483
1484 // if y is negative, adjust variables
1485 if (sy < 0)
1486 {
1487 sh += sy; // subtraction
1488 sy = 0;
1489 }
1490
1491 uint32_t *dst32 = &video.frameBuffer[(sy * SCREEN_W) + sx];
1492 const uint32_t *src32 = s->refreshBuffer;
1493
1494 // handle x/y clipping
1495 if (sx+sw >= SCREEN_W) sw = SCREEN_W - sx;
1496 if (sy+sh >= SCREEN_H) sh = SCREEN_H - sy;
1497
1498 int32_t srcPitch = s->w - sw;
1499 int32_t dstPitch = SCREEN_W - sw;
1500
1501 for (int32_t y = 0; y < sh; y++)
1502 {
1503 for (int32_t x = 0; x < sw; x++)
1504 *dst32++ = *src32++;
1505
1506 src32 += srcPitch;
1507 dst32 += dstPitch;
1508 }
1509 }
1510
1511 fillFromVuMetersBgBuffer(); // let's put it here even though it's not sprite-based
1512 }
1513
1514 void renderSprites(void)
1515 {
1516 renderVuMeters(); // let's put it here even though it's not sprite-based
1517
1518 for (int32_t i = 0; i < SPRITE_NUM; i++)
1519 {
1520 sprite_t *s = &sprites[i];
1521
1522 // set new sprite position
1523 s->x = s->newX;
1524 s->y = s->newY;
1525
1526 if (s->x >= SCREEN_W || s->y >= SCREEN_H) // sprite is hidden, don't draw nor fill clear buffer
1527 continue;
1528
1529 ASSERT(s->data != NULL && s->refreshBuffer != NULL);
1530
1531 int32_t sw = s->w;
1532 int32_t sh = s->h;
1533 int32_t sx = s->x;
1534 int32_t sy = s->y;
1535 int32_t srcPtrBias = 0;
1536
1537 // if x is negative, adjust variables
1538 if (sx < 0)
1539 {
1540 sw += sx; // subtraction
1541 srcPtrBias += -sx;
1542 sx = 0;
1543 }
1544
1545 // if y is negative, adjust variables
1546 if (sy < 0)
1547 {
1548 sh += sy; // subtraction
1549 srcPtrBias += (-sy * s->w);
1550 sy = 0;
1551 }
1552
1553 uint32_t *dst32 = &video.frameBuffer[(sy * SCREEN_W) + sx];
1554 uint32_t *clr32 = s->refreshBuffer;
1555
1556 // handle x/y clipping
1557 if (sx+sw >= SCREEN_W) sw = SCREEN_W - sx;
1558 if (sy+sh >= SCREEN_H) sh = SCREEN_H - sy;
1559
1560 int32_t srcPitch = s->w - sw;
1561 int32_t dstPitch = SCREEN_W - sw;
1562
1563 uint32_t colorKey = sprites[i].colorKey;
1564 if (sprites[i].pixelType == SPRITE_TYPE_RGB)
1565 {
1566 // 24-bit RGB sprite
1567 const uint32_t *src32 = ((uint32_t *)sprites[i].data) + srcPtrBias;
1568 for (int32_t y = 0; y < sh; y++)
1569 {
1570 for (int32_t x = 0; x < sw; x++)
1571 {
1572 *clr32++ = *dst32; // fill clear buffer
1573 if (*src32 != colorKey)
1574 *dst32 = *src32;
1575
1576 dst32++;
1577 src32++;
1578 }
1579
1580 clr32 += srcPitch;
1581 src32 += srcPitch;
1582 dst32 += dstPitch;
1583 }
1584 }
1585 else
1586 {
1587 // 8-bit paletted sprite
1588 const uint8_t *src8 = ((uint8_t *)sprites[i].data) + srcPtrBias;
1589 for (int32_t y = 0; y < sh; y++)
1590 {
1591 for (int32_t x = 0; x < sw; x++)
1592 {
1593 *clr32++ = *dst32; // fill clear buffer
1594 if (*src8 != colorKey)
1595 {
1596 ASSERT(*src8 < PALETTE_NUM);
1597 *dst32 = video.palette[*src8];
1598 }
1599
1600 dst32++;
1601 src8++;
1602 }
1603
1604 clr32 += srcPitch;
1605 src8 += srcPitch;
1606 dst32 += dstPitch;
1607 }
1608 }
1609 }
1610 }
1611
1612 void resetFPSCounter(void)
1613 {
1614 videoFrameCounter = 0;
1615 fpsTextBuf[0] = '\0';
1616 runningFrameDuration = 0;
1617 avgFramesReady = false;
1618 }
1619
1620 void beginFPSCounter(void)
1621 {
1622 if (video.debug)
1623 frameStartTime = SDL_GetPerformanceCounter();
1624 }
1625
1626 void endFPSCounter(void)
1627 {
1628 if (video.debug && frameStartTime > 0)
1629 runningFrameDuration += SDL_GetPerformanceCounter() - frameStartTime;
1630 }
1631
1632 static void drawDebugBox(void)
1633 {
1634 SDL_version SDLVer;
1635
1636 SDL_GetVersion(&SDLVer);
1637
1638 if (++videoFrameCounter >= FPS_SCAN_FRAMES)
1639 {
1640 dAvgFPS = dFrameDurationDiv / (double)runningFrameDuration;
1641 if (dAvgFPS < 0.0 || dAvgFPS > 99999999.9999)
1642 dAvgFPS = 99999999.9999; // prevent number from overflowing text box
1643
1644 runningFrameDuration = 0;
1645 videoFrameCounter = 0;
1646 avgFramesReady = true;
1647 }
1648
1649 drawFramework3(4, 4, SCREEN_W-8, 81);
1650
1651 // if enough frame data isn't collected yet, show a message
1652 if (!avgFramesReady)
1653 {
1654 const char text[] = "Gathering frame information...";
1655 const uint16_t textW = (sizeof (text)-1) * (FONT_CHAR_W-1);
1656 textOut2(4+(SCREEN_W-textW)/2, 4+(81/2) - (FONT_CHAR_H/2), text);
1657 return;
1658 }
1659
1660 double dRefreshRate = video.dMonitorRefreshRate;
1661 if (dRefreshRate < 0.0 || dRefreshRate > 9999.9)
1662 dRefreshRate = 9999.9; // prevent number from overflowing text box
1663
1664 sprintf(fpsTextBuf,
1665 "SDL version: %u.%u.%u\n" \
1666 "Frames per second: %.3f\n" \
1667 "Monitor refresh: %.1fHz (vsync: %s)\n" \
1668 "HPC frequency (timer): %.4fMHz\n" \
1669 "Render size: %dx%d (offset %d,%d)\n" \
1670 "Disp. size: %dx%d (window: %dx%d)\n" \
1671 "Render scaling: x=%.4f, y=%.4f\n" \
1672 "Mouse muls: x=%.4f, y=%.4f\n" \
1673 "Relative mouse coords: %d,%d\n" \
1674 "Absolute mouse coords: %d,%d\n" \
1675 "Press CTRL+SHIFT+F to close this box.\n",
1676 SDLVer.major, SDLVer.minor, SDLVer.patch,
1677 dAvgFPS,
1678 dRefreshRate,
1679 video.vsync60HzPresent ? "yes" : "no",
1680 hpcFreq.freq64 / (1000.0 * 1000.0),
1681 video.renderW, video.renderH, video.renderX, video.renderY,
1682 video.displayW, video.displayH, video.windowW, video.windowH,
1683 (double)video.renderW / SCREEN_W, (double)video.renderH / SCREEN_H,
1684 video.dMouseXMul, video.dMouseYMul,
1685 mouse.x, mouse.y,
1686 mouse.absX, mouse.absY);
1687
1688 // draw text
1689
1690 uint16_t xPos = 4+2;
1691 uint16_t yPos = 4+3;
1692
1693 char *textPtr = fpsTextBuf;
1694 while (*textPtr != '\0')
1695 {
1696 const char ch = *textPtr++;
1697 if (ch == '\n')
1698 {
1699 yPos += FONT_CHAR_H+2;
1700 xPos = 4+2;
1701 continue;
1702 }
1703
1704 charOut(xPos, yPos, ch, 0x00000000);
1705 xPos += FONT_CHAR_W-1;
1706 }
1707
1708 // draw framerate tester symbol
1709
1710 const uint16_t symbolEnd = 122;
1711
1712 // ping-pong movement
1713 uint16_t x = editor.framesPassed % (symbolEnd * 2);
1714 if (x >= symbolEnd)
1715 x = (symbolEnd * 2) - x;
1716
1717 charOut(179+4+x, 4+10, '*', 0x00000000);
1718 }
1719
1720 void flipFrame(void)
1721 {
1722 const uint32_t windowFlags = SDL_GetWindowFlags(video.window);
1723 bool minimized = (windowFlags & SDL_WINDOW_MINIMIZED) ? true : false;
1724
1725 renderSprites();
1726
1727 if (video.debug)
1728 drawDebugBox();
1729
1730 SDL_UpdateTexture(video.texture, NULL, video.frameBuffer, SCREEN_W * sizeof (int32_t));
1731
1732 // SDL 2.0.14 bug on Windows (?): This function consumes ever-increasing memory if the program is minimized
1733 if (!minimized)
1734 SDL_RenderClear(video.renderer);
1735
1736 if (video.useCustomRenderRect)
1737 SDL_RenderCopy(video.renderer, video.texture, NULL, &video.renderRect);
1738 else
1739 SDL_RenderCopy(video.renderer, video.texture, NULL, NULL);
1740
1741 SDL_RenderPresent(video.renderer);
1742
1743 eraseSprites();
1744
1745 if (!video.vsync60HzPresent)
1746 {
1747 // we have no VSync, do crude thread sleeping to sync to ~60Hz
1748 hpc_Wait(&video.vblankHpc);
1749 }
1750 else
1751 {
1752 /* We have VSync, but it can unexpectedly get inactive in certain scenarios.
1753 ** We have to force thread sleeping (to ~60Hz) if so.
1754 */
1755 #ifdef __APPLE__
1756 // macOS: VSync gets disabled if the window is 100% covered by another window. Let's add a (crude) fix:
1757 if (minimized || !(windowFlags & SDL_WINDOW_INPUT_FOCUS))
1758 hpc_Wait(&video.vblankHpc);
1759 #elif __unix__
1760 // *NIX: VSync gets disabled in fullscreen mode (at least on some distros/systems). Let's add a fix:
1761 if (minimized || video.fullscreen)
1762 hpc_Wait(&video.vblankHpc);
1763 #else
1764 if (minimized)
1765 hpc_Wait(&video.vblankHpc);
1766 #endif
1767 }
1768
1769 editor.framesPassed++;
1770
1771 /* Reset audio/video sync timestamp every half an hour to prevent
1772 ** possible sync drifting after hours of playing a song without
1773 ** a single song stop (resets timestamp) in-between.
1774 */
1775 if (editor.framesPassed >= VBLANK_HZ*60*30)
1776 audio.resetSyncTickTimeFlag = true;
1777 }
1778
1779 void updateSpectrumAnalyzer(uint8_t vol, uint16_t period)
1780 {
1781 if (ui.visualizerMode != VISUAL_SPECTRUM || vol == 0)
1782 return;
1783
1784 /* This routine is buggy in real PT. Behavior for periods outside of 108..907 is undefined.
1785 ** Behavior for sane input outside of 113..856 (B-3 finetune > 0 and C-1 finetune < 0)
1786 ** seems to be defined, and we simulate this.
1787 */
1788 if (period < 108 || period > 907)
1789 return; // we don't really know what to do here
1790
1791 // C-1 with finetune < 0 is ignored (confirmed behavior)
1792 if (period > 856)
1793 return;
1794
1795 // B-3 with finetune > 0 behaves like finetune 0 (confirmed behavior)
1796 if (period < 113)
1797 period = 113;
1798
1799 // just in case
1800 if (vol > 64)
1801 vol = 64;
1802
1803 const uint8_t vol24 = (vol * 24) >> 6; // 0..64 -> 0..24
1804
1805 // convert period from log (113..856) to linear (0..22)
1806 period = (856 - 113) - (period - 113); // 0..743 (inverted)
1807 const uint32_t index = (period * period) / 25093; // 0..22 (25093 = round[743^2 / 22])
1808
1809 // increment bar
1810 editor.spectrumVolumes[index] += vol24;
1811 if (editor.spectrumVolumes[index] > SPECTRUM_BAR_HEIGHT)
1812 editor.spectrumVolumes[index] = SPECTRUM_BAR_HEIGHT;
1813
1814 // increment left and right neighbor bars with half the volume
1815
1816 if (index > 0)
1817 {
1818 editor.spectrumVolumes[index-1] += vol24 / 2;
1819 if (editor.spectrumVolumes[index-1] > SPECTRUM_BAR_HEIGHT)
1820 editor.spectrumVolumes[index-1] = SPECTRUM_BAR_HEIGHT;
1821 }
1822
1823 if (index < SPECTRUM_BAR_NUM-1)
1824 {
1825 editor.spectrumVolumes[index+1] += vol24 / 2;
1826 if (editor.spectrumVolumes[index+1] > SPECTRUM_BAR_HEIGHT)
1827 editor.spectrumVolumes[index+1] = SPECTRUM_BAR_HEIGHT;
1828 }
1829 }
1830
1831 void sinkVisualizerBars(void) // sinks visualizer bars
1832 {
1833 // sink VU-meters
1834 for (int32_t i = 0; i < PAULA_VOICES; i++)
1835 {
1836 if (editor.vuMeterVolumes[i] > 0)
1837 editor.vuMeterVolumes[i]--;
1838 }
1839
1840 // sink "spectrum analyzer" bars
1841 for (int32_t i = 0; i < SPECTRUM_BAR_NUM; i++)
1842 {
1843 if (editor.spectrumVolumes[i] > 0)
1844 editor.spectrumVolumes[i]--;
1845 }
1846 }
1847
1848 void updateRenderSizeVars(void)
1849 {
1850 SDL_DisplayMode dm;
1851
1852 int32_t di = SDL_GetWindowDisplayIndex(video.window);
1853 if (di < 0)
1854 di = 0; // return display index 0 (default) on error
1855
1856 SDL_GetDesktopDisplayMode(di, &dm);
1857 video.displayW = dm.w;
1858 video.displayH = dm.h;
1859
1860 SDL_GetWindowSize(video.window, &video.windowW, &video.windowH);
1861 video.renderX = 0;
1862 video.renderY = 0;
1863
1864 video.useCustomRenderRect = false;
1865
1866 if (video.fullscreen)
1867 {
1868 if (config.fullScreenStretch)
1869 {
1870 // "streched out" windowed fullscreen
1871
1872 video.renderW = video.windowW;
1873 video.renderH = video.windowH;
1874 }
1875 else
1876 {
1877 // centered windowed fullscreen, with pixel-perfect integer upscaling
1878
1879 const int32_t maxUpscaleFactor = MIN(video.windowW / SCREEN_W, video.windowH / SCREEN_H);
1880 video.renderW = SCREEN_W * maxUpscaleFactor;
1881 video.renderH = SCREEN_H * maxUpscaleFactor;
1882 video.renderX = (video.windowW - video.renderW) / 2;
1883 video.renderY = (video.windowH - video.renderH) / 2;
1884
1885 // get hi-DPI upscale factors (returns 1.0 if no hi-DPI upscaling)
1886 int32_t widthInPixels, heightInPixels;
1887 SDL_GL_GetDrawableSize(video.window, &widthInPixels, &heightInPixels);
1888 double dHiDPIScaleX = (double)widthInPixels / video.windowW;
1889 double dHiDPIScaleY = (double)heightInPixels / video.windowH;
1890
1891 video.renderRect.x = (int32_t)floor(video.renderX * dHiDPIScaleX);
1892 video.renderRect.y = (int32_t)floor(video.renderY * dHiDPIScaleY);
1893 video.renderRect.w = (int32_t)floor(video.renderW * dHiDPIScaleX);
1894 video.renderRect.h = (int32_t)floor(video.renderH * dHiDPIScaleY);
1895 video.useCustomRenderRect = true; // use the destination coordinates above in SDL_RenderCopy()
1896 }
1897 }
1898 else
1899 {
1900 // windowed mode
1901
1902 SDL_GetWindowSize(video.window, &video.renderW, &video.renderH);
1903 }
1904
1905 // "hardware mouse" calculations
1906 video.mouseCursorUpscaleFactor = MIN(video.renderW / SCREEN_W, video.renderH / SCREEN_H);
1907 createMouseCursors();
1908 }
1909
1910 void toggleFullscreen(void)
1911 {
1912 video.fullscreen ^= 1;
1913 if (video.fullscreen)
1914 SDL_SetWindowFullscreen(video.window, SDL_WINDOW_FULLSCREEN_DESKTOP);
1915 else
1916 SDL_SetWindowFullscreen(video.window, 0);
1917
1918 SDL_Delay(15); // fixes possible issues
1919
1920 updateRenderSizeVars();
1921 updateMouseScaling();
1922
1923 if (video.fullscreen)
1924 {
1925 mouse.setPosX = video.displayW / 2;
1926 mouse.setPosY = video.displayH / 2;
1927 }
1928 else
1929 {
1930 mouse.setPosX = video.renderW / 2;
1931 mouse.setPosY = video.renderH / 2;
1932 }
1933
1934 mouse.setPosFlag = true;
1935 }
1936
1937 bool setupVideo(void)
1938 {
1939 if (config.autoFitVideoScale)
1940 {
1941 int8_t i;
1942 SDL_DisplayMode dm;
1943
1944 int32_t di = SDL_GetWindowDisplayIndex(video.window);
1945 if (di < 0)
1946 di = 0; // return display index 0 (default) on error
1947
1948 // find out which upscaling factor is the biggest to fit on screen
1949 if (SDL_GetDesktopDisplayMode(di, &dm) == 0)
1950 {
1951 for (i = MAX_UPSCALE_FACTOR; i >= 1; i--)
1952 {
1953 // height test is slightly taller because of window title, window borders and taskbar/menu/dock
1954 if (dm.w >= SCREEN_W*i && dm.h >= (SCREEN_H+64)*i)
1955 {
1956 config.videoScaleFactor = i;
1957 break;
1958 }
1959 }
1960
1961 if (i == 0)
1962 config.videoScaleFactor = 1; // 1x is not going to fit, but use 1x anyways...
1963 }
1964 else
1965 {
1966 // couldn't get screen resolution, set to 1x
1967 config.videoScaleFactor = 1;
1968 }
1969 }
1970
1971 int32_t screenW = SCREEN_W * config.videoScaleFactor;
1972 int32_t screenH = SCREEN_H * config.videoScaleFactor;
1973
1974 uint32_t rendererFlags = 0;
1975
1976 SDL_DisplayMode dm;
1977
1978 int32_t di = SDL_GetWindowDisplayIndex(video.window);
1979 if (di < 0)
1980 di = 0; // return display index 0 (default) on error
1981
1982 SDL_GetDesktopDisplayMode(di, &dm);
1983 video.dMonitorRefreshRate = (double)dm.refresh_rate;
1984
1985 video.vsync60HzPresent = false;
1986 if (!config.vsyncOff)
1987 {
1988 if (dm.refresh_rate >= 59 && dm.refresh_rate <= 61)
1989 {
1990 video.vsync60HzPresent = true;
1991 rendererFlags |= SDL_RENDERER_PRESENTVSYNC;
1992 }
1993 }
1994
1995 uint32_t windowFlags = SDL_WINDOW_HIDDEN | SDL_WINDOW_ALLOW_HIGHDPI;
1996
1997 #ifndef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
1998 /* older SDL2 versions don't define this, don't fail the build for it */
1999 #define SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR "SDL_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR"
2000 #endif
2001 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
2002
2003 video.window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED,
2004 SDL_WINDOWPOS_CENTERED, screenW, screenH, windowFlags);
2005
2006 if (video.window == NULL)
2007 {
2008 showErrorMsgBox("Couldn't create SDL window:\n%s", SDL_GetError());
2009 return false;
2010 }
2011
2012 video.renderer = SDL_CreateRenderer(video.window, -1, rendererFlags);
2013 if (video.renderer == NULL)
2014 {
2015 if (video.vsync60HzPresent) // try again without vsync flag
2016 {
2017 video.vsync60HzPresent = false;
2018 rendererFlags &= ~SDL_RENDERER_PRESENTVSYNC;
2019 video.renderer = SDL_CreateRenderer(video.window, -1, rendererFlags);
2020 }
2021
2022 if (video.renderer == NULL)
2023 {
2024 showErrorMsgBox("Couldn't create SDL renderer:\n%s\n\n" \
2025 "Is your GPU (+ driver) too old?", SDL_GetError());
2026 return false;
2027 }
2028 }
2029
2030 SDL_SetRenderDrawBlendMode(video.renderer, SDL_BLENDMODE_NONE);
2031
2032 if (config.pixelFilter == PIXELFILTER_LINEAR)
2033 SDL_SetHint("SDL_RENDER_SCALE_QUALITY", "linear");
2034 else if (config.pixelFilter == PIXELFILTER_BEST)
2035 SDL_SetHint("SDL_RENDER_SCALE_QUALITY", "best");
2036 else
2037 SDL_SetHint("SDL_RENDER_SCALE_QUALITY", "nearest");
2038
2039 video.texture = SDL_CreateTexture(video.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_W, SCREEN_H);
2040 if (video.texture == NULL)
2041 {
2042 showErrorMsgBox("Couldn't create %dx%d GPU texture:\n%s\n\n" \
2043 "Is your GPU (+ driver) too old?", SCREEN_W, SCREEN_H, SDL_GetError());
2044 return false;
2045 }
2046
2047 SDL_SetTextureBlendMode(video.texture, SDL_BLENDMODE_NONE);
2048
2049 // frame buffer used by SDL (for texture)
2050 video.frameBuffer = (uint32_t *)malloc(SCREEN_W * SCREEN_H * sizeof (int32_t));
2051 if (video.frameBuffer == NULL)
2052 {
2053 showErrorMsgBox("Out of memory!");
2054 return false;
2055 }
2056
2057 updateRenderSizeVars();
2058 updateMouseScaling();
2059
2060 if (config.hwMouse)
2061 SDL_ShowCursor(SDL_TRUE);
2062 else
2063 SDL_ShowCursor(SDL_FALSE);
2064
2065 SDL_SetRenderDrawColor(video.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
2066
2067 dFrameDurationDiv = (1000.0 * FPS_SCAN_FRAMES) / hpcFreq.dFreqMulMs;
2068 return true;
2069 }