dos33fsprogs/gr-sim/gr-sim.c

192 lines
2.9 KiB
C
Raw Normal View History

2017-05-02 13:59:39 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <SDL.h>
2017-05-02 14:56:59 +00:00
#include "gr-sim.h"
2017-05-02 13:59:39 +00:00
#define XSIZE 40
#define YSIZE 48
#define PIXEL_X_SCALE 14
#define PIXEL_Y_SCALE 8
static int xsize=XSIZE*PIXEL_X_SCALE;
static int ysize=YSIZE*PIXEL_Y_SCALE;
static unsigned char framebuffer[XSIZE][YSIZE];
2017-05-02 14:56:59 +00:00
/* 128kB of RAM */
unsigned char ram[128*1024];
static SDL_Surface *sdl_screen=NULL;
int grsim_input(void) {
2017-05-02 13:59:39 +00:00
SDL_Event event;
int keypressed;
while ( SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
keypressed=event.key.keysym.sym;
switch (keypressed) {
case SDLK_ESCAPE:
return 'q';
default:
return keypressed;
}
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYAXISMOTION:
default:
printf("Unknown input action!\n");
break;
}
}
return 0;
}
static unsigned int color[16]={
0, /* 0 black */
0xe31e60, /* 1 magenta */
0x604ebd, /* 2 dark blue */
0xff44fd, /* 3 purple */
0x00a360, /* 4 dark green */
0x9c9c9c, /* 5 grey 1 */
0x14cffd, /* 6 medium blue */
0xd0c3ff, /* 7 light blue */
0x607203, /* 8 brown */
0xff6a3c, /* 9 orange */
0x9d9d9d, /* 10 grey 2 */
0xffa0d0, /* 11 pink */
0x14f53c, /* 12 bright green */
0xd0dd8d, /* 13 yellow */
0x72ffd0, /* 14 aqua */
0xffffff, /* 15 white */
};
2017-05-02 14:56:59 +00:00
int grsim_update(void) {
2017-05-02 13:59:39 +00:00
int x,y,i,j;
unsigned int *t_pointer;
2017-05-02 14:56:59 +00:00
t_pointer=((Uint32 *)sdl_screen->pixels);
2017-05-02 13:59:39 +00:00
for(y=0;y<YSIZE;y++) {
for(j=0;j<PIXEL_Y_SCALE;j++) {
for(x=0;x<XSIZE;x++) {
for(i=0;i<PIXEL_X_SCALE;i++) {
*t_pointer=color[framebuffer[x][y]];
t_pointer++;
}
}
}
}
2017-05-02 14:56:59 +00:00
SDL_UpdateRect(sdl_screen, 0, 0, xsize, ysize);
2017-05-02 13:59:39 +00:00
return 0;
}
2017-05-02 14:56:59 +00:00
int grsim_init(void) {
2017-05-02 13:59:39 +00:00
int mode;
int x,y;
mode=SDL_SWSURFACE|SDL_HWPALETTE|SDL_HWSURFACE;
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
return -1;
}
/* Clean up on exit */
atexit(SDL_Quit);
/* assume 32-bit color */
sdl_screen = SDL_SetVideoMode(xsize, ysize, 32, mode);
if ( sdl_screen == NULL ) {
fprintf(stderr, "ERROR! Couldn't set %dx%d video mode: %s\n",
xsize,ysize,SDL_GetError());
return -1;
}
/* Init screen */
for(y=0;y<YSIZE;y++) for(x=0;x<XSIZE;x++) framebuffer[x][y]=0;
2017-05-02 14:56:59 +00:00
return 0;
}
2017-05-02 13:59:39 +00:00
2017-05-02 14:56:59 +00:00
static int current_color=0;
2017-05-02 13:59:39 +00:00
2017-05-02 14:56:59 +00:00
int color_equals(int new_color) {
2017-05-02 19:21:32 +00:00
current_color=new_color%16;
2017-05-02 14:56:59 +00:00
return 0;
}
2017-05-02 13:59:39 +00:00
2017-05-02 14:56:59 +00:00
int plot(int x, int y) {
2017-05-02 19:21:32 +00:00
if (x>40) {
printf("X too big %d\n",x);
return -1;
}
if (y>40) {
printf("Y too big %d\n",y);
return -1;
}
if (x<0) {
printf("X too small %d\n",x);
return -1;
}
if (y<0) {
printf("Y too small %d\n",y);
return -1;
}
2017-05-02 14:56:59 +00:00
framebuffer[x][y]=current_color;
return 0;
}
2017-05-02 13:59:39 +00:00
2017-05-02 14:56:59 +00:00
int hlin(int x1, int x2, int at) {
int i;
for(i=x1;i<x2;i++) plot(i,at);
2017-05-02 13:59:39 +00:00
return 0;
}
2017-05-02 14:56:59 +00:00
int vlin(int y1, int y2, int at) {
int i;
for(i=y1;i<y2;i++) plot(at,i);
return 0;
}
2017-05-02 19:21:32 +00:00
int gr(void) {
int x,y;
2017-05-02 14:56:59 +00:00
2017-05-02 19:21:32 +00:00
/* Init screen */
for(y=0;y<YSIZE;y++) for(x=0;x<XSIZE;x++) framebuffer[x][y]=0;
return 0;
}