forked from makarcz/vm6502
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphDisp.cpp
598 lines (536 loc) · 16.4 KB
/
GraphDisp.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
*--------------------------------------------------------------------
* Project: VM65 - Virtual Machine/CPU emulator programming
* framework.
*
* File: GraphDisp.cpp
*
* Purpose: Implementation of GraphDisp class.
* The GraphDisp class emulates the graphics raster
* device. It handles displaying of the graphics, the
* events loop and the 'hardware' API to the device.
* The higher level abstraction layer - MemMapDev
* defines the actual behavior/response of the emulated
* device in the emulated system.
*
* Date: 8/25/2016
*
* Copyright: (C) by Marek Karcz 2016. All rights reserved.
*
* Contact: makarcz@yahoo.com
*
* License Agreement and Warranty:
This software is provided with No Warranty.
I (Marek Karcz) will not be held responsible for any damage to
computer systems, data or user's health resulting from use.
Please proceed responsibly and apply common sense.
This software is provided in hope that it will be useful.
It is free of charge for non-commercial and educational use.
Distribution of this software in non-commercial and educational
derivative work is permitted under condition that original
copyright notices and comments are preserved. Some 3-rd party work
included with this project may require separate application for
permission from their respective authors/copyright owners.
*--------------------------------------------------------------------
*/
#include "GraphDisp.h"
#include "MKGenException.h"
#include "system.h"
#if defined(WINDOWS)
#include "wtypes.h"
#endif
namespace MKBasic {
/*
*--------------------------------------------------------------------
* Method: GraphDisp()
* Purpose: Class default constructor.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
GraphDisp::GraphDisp()
{
Initialize();
}
/*
*--------------------------------------------------------------------
* Method: GraphDisp()
* Purpose: Class custom constructor.
* Arguments: width, height - integer, graphical display dimensions
* (virtual dimensions as opposed to
* to actual dimensions GRAPHDISP_MAXW,
* GRAPHDISP_MAXH)
* Returns: n/a
*--------------------------------------------------------------------
*/
GraphDisp::GraphDisp(int width, int height)
{
mWidth = width;
mHeight = height;
Initialize();
}
/*
*--------------------------------------------------------------------
* Method: ~GraphDisp()
* Purpose: Class destructor.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
GraphDisp::~GraphDisp()
{
mContLoop = false;
while (mMainLoopActive);
SDL_DestroyRenderer(mpRenderer);
SDL_DestroyWindow(mpWindow);
SDL_Quit();
}
/*
*--------------------------------------------------------------------
* Method: Initialize()
* Purpose: Initialize class members, objects.
* Arguments: n/a
* Returns: n/a
* Problems:
* SDL_CreateWindow with flags:
* SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS|SDL_WINDOW_RESIZABLE
* creates window with no title/icon/system buttons, but with
* a frame, which can be used to resize it. The problem is, when
* it is resized, it stops updating correctly.
*--------------------------------------------------------------------
*/
void GraphDisp::Initialize()
{
int desk_w, desk_h, winbd_top = 5, winbd_right = 5;
mContLoop = true;
mMainLoopActive = false;
mWidth = GRDISP_VR_X; // virtual display width
mHeight = GRDISP_VR_Y; // virtual display height
mPixelSizeX = GRAPHDISP_MAXW / mWidth;
mPixelSizeY = GRAPHDISP_MAXH / mHeight;
mWinPosX = 0; // SDL window position coordinate X
mWinPosY = 0; // SDL window position coordinate Y
mBgRgbR = 0; // bg color, RGB red intensity
mBgRgbG = 0; // bg color, RGB green intensity
mBgRgbB = 0; // bg color, RGB blue intensity
mFgRgbR = 0xFF; // fg color, RGB red intensity
mFgRgbG = 0xFF; // fg color, RGB green intensity
mFgRgbB = 0xFF; // fg color, RGB blue intensity
mpWindow = NULL;
mpSurface = NULL;
mpRenderer = NULL;
GetDesktopResolution(desk_w, desk_h);
// Available in version > 2.0.4
//SDL_GetWindowBordersSize(mpWindow, &winbd_top, NULL, NULL, &winbd_right);
mWinPosX = desk_w - GRAPHDISP_MAXW - winbd_right;
mWinPosY = winbd_top;
SDL_Init(SDL_INIT_VIDEO);
mpWindow = SDL_CreateWindow(
"GraphDisp",
mWinPosX,
mWinPosY,
GRAPHDISP_MAXW,
GRAPHDISP_MAXH,
SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS|SDL_WINDOW_RESIZABLE
);
if (NULL == mpWindow) {
throw MKGenException(SDL_GetError());
}
// Get window surface
mpSurface = SDL_GetWindowSurface(mpWindow);
// Create renderer for window
mpRenderer = SDL_CreateRenderer(mpWindow, -1, SDL_RENDERER_SOFTWARE);
//mpRenderer = SDL_GetRenderer(mpWindow);
if (NULL == mpRenderer) {
throw MKGenException(SDL_GetError());
}
Clear();
}
/*
*--------------------------------------------------------------------
* Method: ClearScreen()
* Purpose: Clear the surface. Update screen.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::ClearScreen()
{
Clear();
UpdateSurface();
}
/*
*--------------------------------------------------------------------
* Method: Clear()
* Purpose: Clear the surface.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::Clear()
{
//Fill the surface with background color
SDL_FillRect(mpSurface, NULL, SDL_MapRGB(mpSurface->format,
mBgRgbR,
mBgRgbG,
mBgRgbB));
}
/*
*--------------------------------------------------------------------
* Method: UpdateSurface()
* Purpose: Update window surface.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::UpdateSurface()
{
//Update the surface
SDL_UpdateWindowSurface(mpWindow);
}
/*
*--------------------------------------------------------------------
* Method: Update()
* Purpose: Update window surface (public).
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::Update()
{
//Update the surface
UpdateSurface();
}
/*
*--------------------------------------------------------------------
* Method: RenderPixel()
* Purpose: Set or unset pixel (scaled) on graphics display
* surface.
* Arguments: x, y - integer, virtual pixel coordinates
* set - boolean, set or unset pixel
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::RenderPixel(int x, int y, bool set)
{
SDL_Rect rtf;
rtf.x = x * mPixelSizeX; rtf.y = y * mPixelSizeY;
rtf.w = mPixelSizeX;
rtf.h = mPixelSizeY;
int rgb_r = 0, rgb_g = 0, rgb_b = 0;
if (set) {
rgb_r = mFgRgbR;
rgb_g = mFgRgbG;
rgb_b = mFgRgbB;
} else {
rgb_r = mBgRgbR;
rgb_g = mBgRgbG;
rgb_b = mBgRgbB;
}
// set or unset pixel
SDL_FillRect(mpSurface, &rtf, SDL_MapRGB(mpSurface->format, rgb_r, rgb_g, rgb_b));
//Update the surface
SDL_UpdateWindowSurface(mpWindow);
}
/*
*--------------------------------------------------------------------
* Method: RendedChar8x8()
* Purpose: Draw 8x8 character from its pixel definition.
* Arguments: chdef - character definition in 8x8 bit matrix
* x, y - coordinates
* reversed - reversed (true) or normal (false)
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::RenderChar8x8(unsigned char chdef[8], int x, int y, bool reversed)
{
SDL_Rect rtf;
int rgb_r = 0, rgb_g = 0, rgb_b = 0;
for (int yy = y, j=0; j < 8; j++, yy++) {
unsigned char chd = chdef[j];
for (int xx = x, i=0; i < 8; i++, xx++) {
bool pixset = (chd & 0x80) == 0x80;
if (reversed) pixset = !pixset;
rtf.x = xx * mPixelSizeX; rtf.y = yy * mPixelSizeY;
rtf.w = mPixelSizeX;
rtf.h = mPixelSizeY;
if (pixset) {
rgb_r = mFgRgbR;
rgb_g = mFgRgbG;
rgb_b = mFgRgbB;
} else {
rgb_r = mBgRgbR;
rgb_g = mBgRgbG;
rgb_b = mBgRgbB;
}
SDL_FillRect(mpSurface, &rtf, SDL_MapRGB(mpSurface->format, rgb_r, rgb_g, rgb_b));
chd = chd << 1; chd &= 0xFE;
}
}
SDL_UpdateWindowSurface(mpWindow);
}
/*
*--------------------------------------------------------------------
* Method: CopyCharRom8x8()
* Purpose: Copy provided 8x8 characters table to internal buffer.
* Arguments: pchrom - pointer to characters defintions table
* Returns:
*--------------------------------------------------------------------
*/
void GraphDisp::CopyCharRom8x8(unsigned char *pchrom)
{
for (int i=0; i<CHROM_8x8_SIZE; i++) {
mCharROM8x8[i] = pchrom[i];
}
}
/*
*--------------------------------------------------------------------
* Method: PrintChar8x8()
* Purpose: Print 8x8 character at specified row and column.
* Arguments: code - character code
* col, row - character coordinates in 8 pixel intervals
* reversed - color mode (reversed or nornal)
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::PrintChar8x8(int code, int col, int row, bool reversed)
{
int x = col * 8;
int y = row * 8;
int n = code * 8;
unsigned char chdef[8];
for (int i=0; i<8; i++) {
chdef[i] = mCharROM8x8[n+i];
}
RenderChar8x8(chdef, x, y, reversed);
}
/*
*--------------------------------------------------------------------
* Method: SetPixel()
* Purpose: Set pixel (scaled) on graphics display surface.
* Arguments: x, y - pixel coordinates
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::SetPixel(int x, int y)
{
RenderPixel(x, y, true);
}
/*
*--------------------------------------------------------------------
* Method: ErasePixel()
* Purpose: Unset (erase) pixel (paint with BG color) on graphics
* display surface.
* Arguments: x, y - pixel coordinates
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::ErasePixel(int x, int y)
{
RenderPixel(x, y, false);
}
/*
*--------------------------------------------------------------------
* Method: DrawLine()
* Purpose: Draw or erase line between specified points.
* Arguments: x1, y1 - coordinates of first point
* x2, y2 - coordinates of second point
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::DrawLine(int x1, int y1, int x2, int y2, bool draworerase)
{
int colR = mFgRgbR, colG = mFgRgbG, colB = mFgRgbB;
if (false == draworerase) {
colR = mBgRgbR;
colG = mBgRgbG;
colB = mBgRgbB;
}
SDL_SetRenderDrawColor(mpRenderer, colR, colG, colB, SDL_ALPHA_OPAQUE);
SDL_RenderSetLogicalSize(mpRenderer, mWidth, mHeight);
SDL_RenderSetScale(mpRenderer, mPixelSizeX, mPixelSizeY);
SDL_RenderDrawLine(mpRenderer, x1, y1, x2, y2);
SDL_RenderPresent(mpRenderer);
}
/*
*--------------------------------------------------------------------
* Method: DrawLine()
* Purpose: Draw line between specified points.
* Arguments: x1, y1 - coordinates of first point
* x2, y2 - coordinates of second point
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::DrawLine(int x1, int y1, int x2, int y2)
{
DrawLine(x1, y1, x2, y2, true);
}
/*
*--------------------------------------------------------------------
* Method: EraseLine()
* Purpose: Erase line between specified points (draw with BG color)
* Arguments: x1, y1 - coordinates of first point
* x2, y2 - coordinates of second point
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::EraseLine(int x1, int y1, int x2, int y2)
{
DrawLine(x1, y1, x2, y2, false);
}
/*
*--------------------------------------------------------------------
* Method: SetBgColor()
* Purpose: Set background color.
* Arguments: r, g, b - integer, red, green and blue intensities
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::SetBgColor(int r, int g, int b)
{
mBgRgbR = r;
mBgRgbG = g;
mBgRgbB = b;
}
/*
*--------------------------------------------------------------------
* Method: SetFgColor()
* Purpose: Set foreground color.
* Arguments: r, g, b - integer, red, green and blue intensities
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::SetFgColor(int r, int g, int b)
{
mFgRgbR = r;
mFgRgbG = g;
mFgRgbB = b;
}
/*
*--------------------------------------------------------------------
* Method: MainLoop()
* Purpose: The main loop to process SDL events and update window.
* This is a global function meant to run in a thread.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void MainLoop(GraphDisp *pgd)
{
pgd->mMainLoopActive = true;
while (pgd->mContLoop) {
pgd->ReadEvents();
pgd->Update();
}
pgd->mMainLoopActive = false;
}
/*
*--------------------------------------------------------------------
* Method: Start()
* Purpose: Starts MainLoop in a thread.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::Start(GraphDisp *pgd)
{
mMainLoopThread = thread(MainLoop,pgd);
}
/*
*--------------------------------------------------------------------
* Method: Stop()
* Purpose: Stop MainLoop thread.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void GraphDisp::Stop()
{
mContLoop = false;
mMainLoopThread.join();
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
bool GraphDisp::IsMainLoopActive()
{
return mMainLoopActive;
}
/*
*--------------------------------------------------------------------
* Method: ReadEvents()
* Purpose: Read events and perform actions when needed.
* Arguments: n/a
* Returns: n/a
* Problems:
* By blitting (copying) surface I wanted to avoid loosing already
* drawn pixels during windows resize, but this doesn't work.
*--------------------------------------------------------------------
*/
void GraphDisp::ReadEvents()
{
SDL_Event e;
SDL_Surface *pTmpSurface;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
mContLoop = false;
break;
case SDL_WINDOWEVENT:
if (SDL_WINDOWEVENT_RESIZED == e.window.event) {
pTmpSurface = SDL_CreateRGBSurface(0,
mpSurface->w,
mpSurface->h,
mpSurface->format->BitsPerPixel,
mpSurface->format->Rmask,
mpSurface->format->Gmask,
mpSurface->format->Bmask,
mpSurface->format->Amask);
SDL_SetWindowSize(mpWindow, GRAPHDISP_MAXW, GRAPHDISP_MAXH);
mpSurface = SDL_GetWindowSurface(mpWindow);
SDL_SetWindowPosition(mpWindow, mWinPosX, mWinPosY);
SDL_SetSurfaceAlphaMod(pTmpSurface, 0);
SDL_BlitSurface(pTmpSurface, 0, mpSurface, 0);
UpdateSurface();
SDL_FreeSurface(pTmpSurface);
} else if (SDL_WINDOWEVENT_FOCUS_GAINED == e.window.event) {
SDL_RaiseWindow(mpWindow);
}
break;
default:
break;
}
}
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void GraphDisp::GetDesktopResolution(int& horizontal, int& vertical)
{
#if defined(WINDOWS)
RECT desktop;
// Get a handle to the desktop window
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
// The top left corner will have coordinates (0,0)
// and the bottom right corner will have coordinates
// (horizontal, vertical)
horizontal = desktop.right;
vertical = desktop.bottom;
#else
horizontal = GRAPHDISP_MAXW;
vertical = GRAPHDISP_MAXH;
#endif
}
} // namespace MKBasic