par_rast

changeset 0:11f024648101 tip

initial
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 22 Mar 2016 00:55:07 +0200
parents
children
files Makefile src/app.c src/app.h src/main_sdl.c src/par3d.h
diffstat 5 files changed, 175 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Tue Mar 22 00:55:07 2016 +0200
     1.3 @@ -0,0 +1,20 @@
     1.4 +src = $(wildcard src/*.c)
     1.5 +obj = $(src:.c=.o)
     1.6 +bin = test
     1.7 +
     1.8 +CFLAGS = -pedantic -Wall -g
     1.9 +LDFLAGS = -lSDL -lm -lpthread
    1.10 +
    1.11 +.PHONY: all
    1.12 +all: .clang_complete $(bin)
    1.13 +
    1.14 +$(bin): $(obj)
    1.15 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.16 +
    1.17 +.clang_complete:
    1.18 +	echo '-x c' >$@
    1.19 +	echo $(CFLAGS) >>$@
    1.20 +
    1.21 +.PHONY: clean
    1.22 +clean:
    1.23 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/app.c	Tue Mar 22 00:55:07 2016 +0200
     2.3 @@ -0,0 +1,36 @@
     2.4 +#include <stdint.h>
     2.5 +#include "app.h"
     2.6 +
     2.7 +int app_init(void)
     2.8 +{
     2.9 +	if(p3d_init() == -1) {
    2.10 +		fprintf(stderr, "failed to initialize par3d\n");
    2.11 +		return -1;
    2.12 +	}
    2.13 +	return 0;
    2.14 +}
    2.15 +
    2.16 +void app_cleanup(void)
    2.17 +{
    2.18 +}
    2.19 +
    2.20 +void app_draw(void)
    2.21 +{
    2.22 +	int i, j;
    2.23 +	uint32_t *pptr = fb_pixels;
    2.24 +	for(i=0; i<fb_height; i++) {
    2.25 +		for(j=0; j<fb_width; j++) {
    2.26 +			unsigned char val = i ^ j;
    2.27 +			*pptr++ = val | (val << 8) | (val << 16);
    2.28 +		}
    2.29 +	}
    2.30 +
    2.31 +	p3d_setfb(fb_width, fb_height, fb_pixels);
    2.32 +}
    2.33 +
    2.34 +void app_keyboard(int key, int pressed)
    2.35 +{
    2.36 +	if(key == 27) {
    2.37 +		app_quit();
    2.38 +	}
    2.39 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/app.h	Tue Mar 22 00:55:07 2016 +0200
     3.3 @@ -0,0 +1,16 @@
     3.4 +#ifndef APP_H_
     3.5 +#define APP_H_
     3.6 +
     3.7 +void *fb_pixels;
     3.8 +int fb_width, fb_height;
     3.9 +
    3.10 +int app_init(void);
    3.11 +void app_cleanup(void);
    3.12 +
    3.13 +void app_draw(void);
    3.14 +void app_keyboard(int key, int pressed);
    3.15 +
    3.16 +/* stuff defined in main_sdl.c */
    3.17 +void app_quit(void);
    3.18 +
    3.19 +#endif	/* APP_H_ */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/main_sdl.c	Tue Mar 22 00:55:07 2016 +0200
     4.3 @@ -0,0 +1,92 @@
     4.4 +#include <stdio.h>
     4.5 +#include <stdlib.h>
     4.6 +#include <SDL/SDL.h>
     4.7 +#include "app.h"
     4.8 +
     4.9 +static int handle_event(SDL_Event *ev);
    4.10 +
    4.11 +static SDL_Surface *fbsurf;
    4.12 +static int quit;
    4.13 +
    4.14 +int main(int argc, char **argv)
    4.15 +{
    4.16 +	unsigned long msec, prev_fps_upd;
    4.17 +	unsigned int num_frames, framerate;
    4.18 +
    4.19 +	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
    4.20 +		fprintf(stderr, "failed to initialize SDL\n");
    4.21 +		return 1;
    4.22 +	}
    4.23 +	if(!(fbsurf = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE | SDL_RESIZABLE))) {
    4.24 +		fprintf(stderr, "failed to create surface\n");
    4.25 +		SDL_Quit();
    4.26 +		return 1;
    4.27 +	}
    4.28 +	SDL_WM_SetCaption("parallel rasterization test", 0);
    4.29 +
    4.30 +	if(app_init() == -1) {
    4.31 +		return 1;
    4.32 +	}
    4.33 +
    4.34 +	prev_fps_upd = SDL_GetTicks();
    4.35 +	num_frames = 0;
    4.36 +
    4.37 +	while(!quit) {
    4.38 +		unsigned long dt;
    4.39 +		SDL_Event ev;
    4.40 +		while(SDL_PollEvent(&ev)) {
    4.41 +			if(handle_event(&ev) == -1 || quit) {
    4.42 +				goto break_main_loop;
    4.43 +			}
    4.44 +		}
    4.45 +
    4.46 +		if(SDL_MUSTLOCK(fbsurf)) {
    4.47 +			SDL_LockSurface(fbsurf);
    4.48 +		}
    4.49 +		fb_pixels = fbsurf->pixels;
    4.50 +		fb_width = fbsurf->w;
    4.51 +		fb_height = fbsurf->h;
    4.52 +
    4.53 +		app_draw();
    4.54 +
    4.55 +		if(SDL_MUSTLOCK(fbsurf)) {
    4.56 +			SDL_UnlockSurface(fbsurf);
    4.57 +		}
    4.58 +		SDL_Flip(fbsurf);
    4.59 +
    4.60 +		++num_frames;
    4.61 +		msec = SDL_GetTicks();
    4.62 +		dt = msec - prev_fps_upd;
    4.63 +		if(dt > 1500) {
    4.64 +			char tmp[32];
    4.65 +			framerate = 10000 * num_frames / dt;
    4.66 +			num_frames = 0;
    4.67 +			prev_fps_upd = msec;
    4.68 +
    4.69 +			sprintf(tmp, "fps: %u.%u\n", framerate / 10, framerate % 10);
    4.70 +			SDL_WM_SetCaption(tmp, 0);
    4.71 +		}
    4.72 +	}
    4.73 +break_main_loop:
    4.74 +	app_cleanup();
    4.75 +	SDL_Quit();
    4.76 +	return 0;
    4.77 +}
    4.78 +
    4.79 +void app_quit(void)
    4.80 +{
    4.81 +	quit = 1;
    4.82 +}
    4.83 +
    4.84 +static int handle_event(SDL_Event *ev)
    4.85 +{
    4.86 +	switch(ev->type) {
    4.87 +	case SDL_KEYDOWN:
    4.88 +		app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
    4.89 +		break;
    4.90 +
    4.91 +	default:
    4.92 +		break;
    4.93 +	}
    4.94 +	return 0;
    4.95 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/par3d.h	Tue Mar 22 00:55:07 2016 +0200
     5.3 @@ -0,0 +1,11 @@
     5.4 +#ifndef PAR3D_H_
     5.5 +#define PAR3D_H_
     5.6 +
     5.7 +int p3d_init(void);
     5.8 +void p3d_setfb(int width, int height, uint32_t *pixels);
     5.9 +
    5.10 +int p3d_vertex_buffer(int vcount, int nfloats);
    5.11 +void *p3d_map_buffer(int vb);
    5.12 +void p3d_unmap_buffer(int vb);
    5.13 +
    5.14 +#endif	/* PAR3D_H_ */