# HG changeset patch # User John Tsiombikas # Date 1513322679 -7200 # Node ID 224206bc554a11cfc8d5627cc87a5ea326b1369e initial commit diff -r 000000000000 -r 224206bc554a .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Dec 15 09:24:39 2017 +0200 @@ -0,0 +1,4 @@ +\.o$ +\.swp$ +\.d$ +^test$ diff -r 000000000000 -r 224206bc554a Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Dec 15 09:24:39 2017 +0200 @@ -0,0 +1,23 @@ +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +dep = $(obj:.o=.d) +bin = test + +CFLAGS = -pedantic -Wall -g `sdl-config --cflags` +LDFLAGS = `sdl-config --libs` + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +-include $(dep) + +%.d: %.c + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +.PHONY: clean +clean: + rm -f $(obj) $(bin) + +.PHONY: cleandep +cleandep: + rm -f $(dep) diff -r 000000000000 -r 224206bc554a src/main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.c Fri Dec 15 09:24:39 2017 +0200 @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include "poly.h" +#include "mgl.h" + +int init(void); +void cleanup(void); +void draw(void); +void process_event(SDL_Event *ev); +void handle_key_event(int key, int pressed); + +int win_width, win_height; +int fb_width = 80, fb_height = 60; +int fb_scale = 16; +uint32_t *fb_pixels; +SDL_Surface *surf; +int quit; + +int opt_grid = 1; + +int main(int argc, char **argv) +{ + win_width = fb_width * fb_scale; + win_height = fb_height * fb_scale; + + if(SDL_Init(SDL_INIT_VIDEO) == -1) { + return 1; + } + + if(!(surf = SDL_SetVideoMode(win_width, win_height, 32, SDL_SWSURFACE))) { + fprintf(stderr, "failed to set video mode: %dx%d\n", win_width, win_height); + return 1; + } + SDL_WM_SetCaption("anotherpoly", 0); + + if(init() == -1) { + return 1; + } + + for(;;) { + SDL_Event ev; + while(SDL_PollEvent(&ev)) { + process_event(&ev); + if(quit) goto done; + } + + draw(); + } + +done: + cleanup(); + SDL_Quit(); + return 0; +} + + +int init(void) +{ + if(!(fb_pixels = malloc(fb_width * fb_height * sizeof *fb_pixels))) { + perror("failed to allocate framebuffer"); + return -1; + } + if(polyfill_framebuffer(fb_pixels, fb_width, fb_height) == -1) { + return -1; + } + + return 0; +} + +void cleanup(void) +{ + free(fb_pixels); +} + +void draw(void) +{ + int i, j; + uint32_t *dest, *src; + + memset(fb_pixels, 0, fb_width * fb_height * sizeof *fb_pixels); + + mgl_begin(MGL_TRIANGLES); + mgl_vertex3f(40, 8, 0); + mgl_vertex3f(15, 35, 0); + mgl_vertex3f(65, 50, 0); + mgl_end(); + + if(SDL_MUSTLOCK(surf)) { + SDL_LockSurface(surf); + } + dest = surf->pixels; + src = fb_pixels; + + for(i=0; itype) { + case SDL_QUIT: + quit = 1; + break; + + case SDL_KEYDOWN: + handle_key_event(ev->key.keysym.sym, 1); + break; + + default: + break; + } +} + +void handle_key_event(int key, int pressed) +{ + if(pressed) { + switch(key) { + case SDLK_ESCAPE: + quit = 1; + break; + + case 'g': + opt_grid = !opt_grid; + break; + + default: + break; + } + } +} diff -r 000000000000 -r 224206bc554a src/mgl.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mgl.c Fri Dec 15 09:24:39 2017 +0200 @@ -0,0 +1,75 @@ +#include "mgl.h" +#include "poly.h" + +struct vertex { + int32_t x, y, z; + int32_t u, v; +}; + +static void proc_prim(void); + +static int cur_prim; +static int vidx; +static struct vertex varr[4]; +static int32_t cur_u, cur_v; + +void mgl_begin(int prim) +{ + cur_prim = prim; + vidx = 0; +} + +void mgl_end(void) +{ + if(cur_prim) { + proc_prim(); + } + cur_prim = 0; +} + +void mgl_vertex3f(float x, float y, float z) +{ + mgl_vertex3x((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f), (int32_t)(z * 65536.0f)); +} + +void mgl_vertex3x(int32_t x, int32_t y, int32_t z) +{ + varr[vidx].x = x; + varr[vidx].y = y; + varr[vidx].z = z; + varr[vidx].u = cur_u; + varr[vidx].v = cur_v; + + if(++vidx >= cur_prim) { + proc_prim(); + vidx = 0; + } +} + +void mgl_texcoord2f(float u, float v) +{ + cur_u = (int32_t)(u * 65536.0); + cur_v = (int32_t)(v * 65536.0); +} + +void mgl_texcoord2x(int32_t u, int32_t v) +{ + cur_u = u; + cur_v = v; +} + +void proc_prim(void) +{ + int i; + struct poly_vertex v[4]; + + for(i=0; i + +enum { + MGL_POINTS = 1, + MGL_LINES = 2, + MGL_TRIANGLES = 3, + MGL_QUADS = 4 +}; + +void mgl_begin(int prim); +void mgl_end(void); + +void mgl_vertex3f(float x, float y, float z); +void mgl_vertex3x(int32_t x, int32_t y, int32_t z); +void mgl_texcoord2f(float u, float v); +void mgl_texcoord2x(int32_t u, int32_t v); + +#endif /* MGL_H_ */ diff -r 000000000000 -r 224206bc554a src/poly.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/poly.c Fri Dec 15 09:24:39 2017 +0200 @@ -0,0 +1,47 @@ +#include +#include +#include +#include "poly.h" + +static uint32_t *fb_pixels; +static int fb_width, fb_height; +static unsigned int *scanoffs; +static int scanoffs_size; + +int polyfill_framebuffer(void *fb, int width, int height) +{ + int i; + unsigned int offs = 0; + + if(height > scanoffs_size) { + free(scanoffs); + scanoffs_size = height; + if(!(scanoffs = malloc(scanoffs_size * sizeof *scanoffs))) { + perror("failed to allocate scanline offset table"); + return -1; + } + } + + for(i=0; i> 16; + int y = varr[i].y >> 16; + + fb_pixels[scanoffs[y] + x] = 0xff0000; + } +} diff -r 000000000000 -r 224206bc554a src/poly.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/poly.h Fri Dec 15 09:24:39 2017 +0200 @@ -0,0 +1,15 @@ +#ifndef POLY_H_ +#define POLY_H_ + +#include + +struct poly_vertex { + int32_t x, y, z; /* 16.16 */ + int32_t u, v; /* 16.16 */ +}; + +int polyfill_framebuffer(void *fb, int width, int height); + +void polyfill_flat(struct poly_vertex *varr, int vcount); + +#endif /* POLY_H_ */