-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode13h.c
172 lines (154 loc) · 4.65 KB
/
mode13h.c
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
// Mode13h - Mode 13h graphics wrapper for SDL
// Copyright 1999-2010, Ibán Cereijo Graña <ibancg at gmail dot com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "mode13h.h"
#ifdef DOS
# include <conio.h>
# include <dpmi.h>
#endif
screen_t* startGraph(int double_buffer) {
screen_t* result = malloc(sizeof(screen_t));
result->w = 320;
result->h = 200;
result->size = result->w * result->h;
# ifdef DOS
_go32_dpmi_registers regs;
regs.x.ax = 0x13;
__dpmi_simulate_real_mode_interrupt(0x10, ®s);
result->vid_selector = __dpmi_segment_to_descriptor(0xA000);
result->virtual = NULL;
if (double_buffer) {
result->virtual = (unsigned char *) malloc(result->size);
_go32_dpmi_lock_data(result->virtual, result->size);
memset(result->virtual, 0, result->size);
}
// the pixels are not squares
result->pointer = result->virtual;
result->aspect_ratio_correction = 5.0 / 6.0;
# else
result->surface = SDL_SetVideoMode(result->w, result->h, 8, SDL_HWSURFACE
| SDL_DOUBLEBUF);
// the pixels are squares when they are rendered in a X window
result->aspect_ratio_correction = 1.0;
if (result->surface == NULL) {
free(result);
result = NULL;
} else {
result->pointer = result->surface->pixels;
}
// memset(result->pointer, 100, 64000);
# endif
return result;
}
void stopGraph(screen_t* screen) {
# ifdef DOS
if (screen->virtual != NULL) {
free(screen->virtual);
}
_go32_dpmi_registers regs;
regs.x.ax = 0x3;
__dpmi_simulate_real_mode_interrupt(0x10, ®s);
# else
SDL_Quit();
# endif
free(screen);
}
unsigned int checkStopEvent(screen_t* screen) {
unsigned int stop = 0;
# ifdef DOS
stop = kbhit();
# else
SDL_Event event;
while (SDL_PollEvent(&event)) {
if ((event.type == SDL_QUIT) /*|| (event.type == SDL_KEYUP)*/) {
stop = 1;
}
}
# endif
return stop;
}
inline void putPixel(screen_t* screen, int x, int y, unsigned char color) {
if ((x >= 0) && (x < screen->w) && (y >= 0) && (y < screen->h)) {
# ifdef DOS
if (screen->virtual != NULL) {
*(((unsigned char*) screen->pointer) + (y << 6) + (y << 8) + x)
= color;
} else {
_farpokeb(screen->vid_selector, (y << 6) + (y << 8) + x, color);
}
# else
char *buffer = (char*) screen->surface->pixels + screen->surface->pitch
* y + x * screen->surface->format->BytesPerPixel;
// if (SDL_MUSTLOCK(screen))
// SDL_LockSurface(screen);
*buffer = color;
// if (SDL_MUSTLOCK(screen))
// SDL_UnlockSurface(screen);
# endif
}
}
unsigned char getPixel(screen_t* screen, int x, int y) {
if ((x >= 0) && (x < screen->w) && (y >= 0) && (y < screen->h)) {
# ifdef DOS
if (screen->virtual != NULL) {
return *(((unsigned char*) screen->pointer) + (y << 6) + (y << 8)
+ x);
} else {
return _farpeekb(screen->vid_selector, (y << 6) + (y << 8) + x);
}
# else
char *buffer = (char*) screen->surface->pixels + screen->surface->pitch
* y + x * screen->surface->format->BytesPerPixel;
// if (SDL_MUSTLOCK(screen))
// SDL_LockSurface(screen);
return *buffer;
// if (SDL_MUSTLOCK(screen))
// SDL_UnlockSurface(screen);
# endif
}
return 0;
}
void defineColor(screen_t* screen, unsigned char color_index, unsigned char r,
unsigned char g, unsigned char b) {
# ifdef DOS
outportb(0x3C8, color_index);
outportb(0x3C9, r >> 2);
outportb(0x3C9, g >> 2);
outportb(0x3C9, b >> 2);
# else
screen->colors[color_index].r = r;
screen->colors[color_index].g = g;
screen->colors[color_index].b = b;
# endif
}
void setPalette(screen_t* screen) {
# ifdef DOS
# else
SDL_SetColors(screen->surface, screen->colors, 0, 256);
# endif
}
void flip(screen_t* screen) {
# ifdef DOS
if (screen->virtual != NULL) {
//_movedatal(_my_ds(), screen->virtual, screen->vid_selector, 0, 16000);
int i;
unsigned long int* p = screen->virtual;
for (i = 0; i < screen->size; i += 4)
_farpokel(screen->vid_selector, i, *p++);
}
# else
SDL_Flip(screen->surface);
# endif
}