Plan 9 from Bell Labs’s /usr/web/sources/contrib/aiju/nes/gfx.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <SDL.h>
#include "6502.h"
#include "gfx.h"
#include "sys.h"

extern int buttons, pr;
SDL_Surface* display;

void drawpixel(int x, int y, int c) {
	int* p = display->pixels;
	if(x >= 256 || y >= 240) return;
	p[x + y * 256] = c;
}

int frames = 0;

const int key[8] = {
	'a',
	'b',
	SDLK_TAB,
	SDLK_RETURN,
	SDLK_UP,
	SDLK_DOWN,
	SDLK_LEFT,
	SDLK_RIGHT};

void updatedisplay() {
	int i;
	SDL_Flip(display);
	frames++;
	SDL_Event e;
	while(SDL_PollEvent(&e)) {
		switch(e.type) {
			case SDL_QUIT: exit(0);
			case SDL_KEYDOWN: 
				for(i=0;i<8;i++)
					if(key[i] == e.key.keysym.sym)
						buttons |= (1<<i);
				if(e.key.keysym.sym == '0') pr = 1; 
				if(e.key.keysym.sym == 'p') save();
			break;
			case SDL_KEYUP: for(i=0;i<8;i++) if(key[i] == e.key.keysym.sym) buttons &= ~(1<<i); break;
		}
	}
}

void alhandler() {
	char buf[512];
	snprintf(buf, 511, "%d FPS", frames);
	SDL_WM_SetCaption(buf, buf);
}


void gfxinit() {
	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
		die("can't initialize SDL: %s", SDL_GetError());
	display = SDL_SetVideoMode(256, 240, 32, 0);
	signal(SIGALRM, alhandler);
	alarm(1);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].