1
0
mirror of https://github.com/aap/pdp6.git synced 2026-04-26 20:26:42 +00:00

emu: ported to SDL2

This commit is contained in:
aap
2019-03-10 00:01:37 +01:00
parent 2cf5501709
commit 83b757ae1c
5 changed files with 137 additions and 123 deletions

View File

@@ -3,9 +3,9 @@ SRC=panel6.c ../emu/util.c
#CFLAGS= -Wno-shift-op-parentheses -Wno-logical-op-parentheses \
# -Wno-bitwise-op-parentheses
CFLAGS= -g -fno-diagnostics-show-caret \
`sdl-config --cflags` `pkg-config SDL_image --cflags`
`sdl2-config --cflags` `pkg-config SDL2_image --cflags`
LIBS= `sdl-config --libs` `pkg-config SDL_image --libs` -lpthread
LIBS= `sdl2-config --libs` `pkg-config SDL2_image --libs` -lpthread
panel6: $(SRC)

View File

@@ -1,7 +1,7 @@
#include <stdarg.h>
#include <fcntl.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <pthread.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
@@ -28,10 +28,17 @@ struct Point
float x, y;
};
typedef struct Image Image;
struct Image
{
SDL_Texture *tex;
int w, h;
};
typedef struct Panel Panel;
struct Panel
{
SDL_Surface *surf;
Image *surf;
SDL_Rect pos;
};
@@ -46,7 +53,7 @@ struct Grid
typedef struct Element Element;
struct Element
{
SDL_Surface **surf;
Image **surf;
Grid *grid;
Point pos;
int state;
@@ -65,19 +72,26 @@ err(char *fmt, ...)
exit(1);
}
SDL_Surface*
SDL_Window *window;
SDL_Renderer *renderer;
Image*
mustloadimg(const char *path)
{
SDL_Surface *s;
s = IMG_Load(path);
Image *img;
SDL_Texture *s;
s = IMG_LoadTexture(renderer, path);
if(s == nil)
err("Couldn't load %s", path);
return s;
img = malloc(sizeof(Image));
img->tex = s;
SDL_QueryTexture(img->tex, nil, nil, &img->w, &img->h);
return img;
}
SDL_Surface *lampsurf[2];
SDL_Surface *switchsurf[2];
SDL_Surface *keysurf[3];
Image *lampsurf[2];
Image *switchsurf[2];
Image *keysurf[3];
Panel oppanel;
Grid opgrid1; /* the smaller base grid */
@@ -188,32 +202,6 @@ get36switches(uchar *b, Element *sw)
b[4] = getswitches(sw+32, 4, 1);
}
void
putpixel(SDL_Surface *screen, int x, int y, Uint32 col)
{
Uint32 *p = (Uint32*)screen->pixels;
if(x < 0 || x >= screen->w ||
y < 0 || y >= screen->h)
return;
p += y*screen->w+x;
*p = SDL_MapRGBA(screen->format,
col&0xFF, (col>>8)&0xFF, (col>>16)&0xFF, (col>>24)&0xFF);
}
void
drawhline(SDL_Surface *screen, int y, int x1, int x2, Uint32 col)
{
for(; x1 < x2; x1++)
putpixel(screen, x1, y, col);
}
void
drawvline(SDL_Surface *screen, int x, int y1, int y2, Uint32 col)
{
for(; y1 < y2; y1++)
putpixel(screen, x, y1, col);
}
Point
xform(Grid *g, Point p)
{
@@ -236,31 +224,21 @@ ismouseover(Element *e, int x, int y)
}
void
drawgrid(Grid *g, SDL_Surface *s, Uint32 col)
drawpanel(SDL_Renderer *renderer, Panel *p)
{
SDL_Surface *ps;
int x, y;
int xmax, ymax;
Point p;
if(p->pos.x < 0)
return;
ps = g->panel->surf;
xmax = ps->w/g->xscl;
ymax = ps->h/g->yscl;
for(x = 0; x < xmax; x++){
p = xform(g, (Point){ x, 0 });
drawvline(s, p.x,
p.y - ps->h, p.y, col);
}
for(y = 0; y < ymax; y++){
p = xform(g, (Point){ 0, y });
drawhline(s, p.y,
p.x, p.x + ps->w, col);
}
p->pos.w = p->surf->w;
p->pos.h = p->surf->h;
SDL_RenderCopy(renderer, p->surf->tex, nil, &p->pos);
}
void
drawelement(SDL_Surface *screen, Element *elt, int power)
drawelement(SDL_Renderer *renderer, Element *elt, int power)
{
Image *img;
SDL_Rect r;
Point p;
int s;
@@ -275,7 +253,10 @@ drawelement(SDL_Surface *screen, Element *elt, int power)
s = elt->state && power;
else
s = elt->state;
SDL_BlitSurface(elt->surf[s], nil, screen, &r);
img = elt->surf[s];
r.w = img->w;
r.h = img->h;
SDL_RenderCopy(renderer, img->tex, nil, &r);
}
void
@@ -597,7 +578,6 @@ talki2c(void *x)
int
main(int argc, char *argv[])
{
SDL_Surface *screen;
SDL_Event ev;
SDL_MouseButtonEvent *mbev;
SDL_MouseMotionEvent *mmev;
@@ -606,8 +586,11 @@ main(int argc, char *argv[])
int w, h;
pthread_t comthread;
if(SDL_Init(SDL_INIT_VIDEO) < 0)
err("%s", SDL_GetError());
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
if(SDL_CreateWindowAndRenderer(1399, 200, 0, &window, &renderer) < 0)
err("SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
lampsurf[0] = mustloadimg("../art/lamp_off.png");
lampsurf[1] = mustloadimg("../art/lamp_on.png");
@@ -654,9 +637,7 @@ main(int argc, char *argv[])
// findlayout(&w, &h);
findlayout_small(&w, &h);
screen = SDL_SetVideoMode(w, h, 32, SDL_DOUBLEBUF);
if(screen == nil)
err("%s", SDL_GetError());
SDL_SetWindowSize(window, w, h);
e = switches;
data_sw = e; e += 36;
@@ -716,27 +697,23 @@ main(int argc, char *argv[])
return 0;
}
SDL_FillRect(screen, nil, SDL_MapRGBA(screen->format, 0xe6, 0xe6, 0xe6, 0xff));
if(indpanel1.pos.x >= 0)
SDL_BlitSurface(indpanel1.surf, nil, screen, &indpanel1.pos);
if(indpanel2.pos.x >= 0)
SDL_BlitSurface(indpanel2.surf, nil, screen, &indpanel2.pos);
if(extrapanel.pos.x >= 0)
SDL_BlitSurface(extrapanel.surf, nil, screen, &extrapanel.pos);
if(iopanel.pos.x >= 0)
SDL_BlitSurface(iopanel.surf, nil, screen, &iopanel.pos);
if(oppanel.pos.x >= 0)
SDL_BlitSurface(oppanel.surf, nil, screen, &oppanel.pos);
SDL_SetRenderDrawColor(renderer, 0xe6, 0xe6, 0xe6, 0xff);
SDL_RenderClear(renderer);
drawpanel(renderer, &indpanel1);
drawpanel(renderer, &indpanel2);
drawpanel(renderer, &extrapanel);
drawpanel(renderer, &iopanel);
drawpanel(renderer, &oppanel);
for(i = 0, e = lamps; i < nelem(lamps); i++, e++)
drawelement(screen, e, 1);
drawelement(renderer, e, 1);
for(i = 0, e = keys; i < nelem(keys); i++, e++)
drawelement(screen, e, 1);
drawelement(renderer, e, 1);
for(i = 0, e = switches; i < nelem(switches); i++, e++)
drawelement(screen, e, 1);
drawelement(renderer, e, 1);
SDL_Flip(screen);
SDL_RenderPresent(renderer);
}
return 0;
}