libdgl/DGLGFX.C
Gered 62af8575c6 various updates i've left uncommitted for many months
- rename standard integer types to a more familiar (u)int(8/16/32)
- many function/struct renames. i don't _really_ know if what i've done
  for this is better, but it "feels" better to me. many draw/blit
  function names are shorter which is nice, at least. kinda important
  to me because i develop this on a real DOS machine in 80x50 text mode.
- add 'extern "C"' blocks to headers for C++ compiler usage
- draw/pixel color value arguments for functions should all have been
  changed to be uint8 instead of a full 32-bit int. feels right, but
  maybe should've left alone...
- small fix to keyboard handler. noticed a problem on one thinkpad
  laptop. was a result of what i think was a typo in a constant value
  used during the part of the interrupt handler that tells the keyboard
  controller the key event was processed
- fix uncommon potential crash function return in draw_filled_rect
- renamed low-level "direct" assembly functions to "lowlevel_xxx" to
  be a little bit more explicit about what they are
- add some convenience event helper functions for determining event
  types
- add fixed point atan2
- fixed some tabs/spaces inconsistences (should all be spaces now?)
- maybe some other minor things i've forgotten
2020-07-19 19:24:48 -04:00

104 lines
2.3 KiB
C
Executable file

#include "dgl.h"
#include "dglgfx.h"
#include "dglblit.h"
#include "dglutil.h"
#include <conio.h>
#include <stdlib.h>
#include <string.h>
extern void set_video_mode(int mode);
#pragma aux set_video_mode = \
"int 0x10" \
parm [eax];
static bool _initialized = false;
SURFACE *screen = NULL;
static SURFACE* surface_create_internal(int width, int height, uint8 *pixels) {
SURFACE *surface = (SURFACE*)malloc(sizeof(SURFACE));
surface->width = width;
surface->height = height;
surface->clip_region = rect(0, 0, width, height);
surface->flags = 0;
if (pixels != NULL) {
surface->flags |= SURFACE_FLAGS_ALIASED;
surface->pixels = pixels;
} else {
int size = width * height;
surface->pixels = (uint8*)malloc(size);
mem_fill(surface->pixels, 0, size);
}
return surface;
}
bool gfx_init(void) {
if (_initialized) {
dgl_set_error(DGL_VIDEO_ALREADY_INITIALIZED);
return false;
}
set_video_mode(0x13);
screen = surface_create_internal(320, 200, (uint8*)0xa0000);
surface_clear(screen, 0);
_initialized = true;
return true;
}
bool gfx_shutdown(void) {
if (!_initialized)
return true; // don't care
set_video_mode(0x03);
surface_free(screen);
screen = NULL;
_initialized = false;
return true;
}
bool gfx_is_initialized(void) {
return _initialized;
}
void wait_vsync(void) {
do {} while (inp(0x3da) & 0x8);
do {} while (!(inp(0x3da) & 0x8));
}
SURFACE* surface_create(int width, int height) {
return surface_create_internal(width, height, NULL);
}
void surface_free(SURFACE *surface) {
if (!surface)
return;
if (!BIT_ISSET(SURFACE_FLAGS_ALIASED, surface->flags))
free(surface->pixels);
free(surface);
}
void surface_clear(SURFACE *surface, uint8 color) {
int length = surface->width * surface->height;
mem_fill(surface->pixels, color, length);
}
void surface_copy(const SURFACE *src, SURFACE *dest) {
if (src->width == dest->width && src->height == dest->height) {
int length = src->width * src->height;
mem_copy(dest->pixels, src->pixels, length);
} else {
blit(src, dest, 0, 0);
}
}