deepstone

changeset 14:be61704c4cc8

added initial firstp
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 29 Nov 2011 07:22:49 +0200
parents 7b574ba5758e
children 4a0e9ab12ad0
files Makefile firstp/Makefile firstp/Makefile.bcc firstp/firstp.c src/mglimpl.h
diffstat 5 files changed, 310 insertions(+), 11 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Mon Nov 28 05:12:33 2011 +0200
     1.2 +++ b/Makefile	Tue Nov 29 07:22:49 2011 +0200
     1.3 @@ -1,6 +1,7 @@
     1.4  obj = src/test.o \
     1.5  	  src/mingl.o src/mglrast.o src/mglgen.o \
     1.6  	  src/texture.o src/palman.o \
     1.7 +	  src/scene.o src/cvec.o \
     1.8  	  dosemu/dosemu.o
     1.9  dep = $(obj:.o=.d)
    1.10  bin = test
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/firstp/Makefile	Tue Nov 29 07:22:49 2011 +0200
     2.3 @@ -0,0 +1,30 @@
     2.4 +mgldir = ../src
     2.5 +demudir = ../dosemu
     2.6 +
     2.7 +obj = firstp.o \
     2.8 +	  $(mgldir)/mingl.o $(mgldir)/mglrast.o $(mgldir)/mglgen.o \
     2.9 +	  $(mgldir)/texture.o $(mgldir)/palman.o \
    2.10 +	  $(mgldir)/cvec.o $(mgldir)/scene.o \
    2.11 +	  $(demudir)/dosemu.o
    2.12 +dep = $(obj:.o=.d)
    2.13 +bin = firstp
    2.14 +
    2.15 +CC = gcc
    2.16 +CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl` -I$(mgldir) -I$(demudir)
    2.17 +LDFLAGS = `pkg-config --libs sdl`
    2.18 +
    2.19 +$(bin): $(obj)
    2.20 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    2.21 +
    2.22 +-include $(dep)
    2.23 +
    2.24 +%.d: %.c
    2.25 +	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.26 +
    2.27 +.PHONY: clean
    2.28 +clean:
    2.29 +	rm -f $(obj) $(bin)
    2.30 +
    2.31 +.PHONY: cleandep
    2.32 +cleandep:
    2.33 +	rm -f $(dep)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/firstp/Makefile.bcc	Tue Nov 29 07:22:49 2011 +0200
     3.3 @@ -0,0 +1,26 @@
     3.4 +.AUTODEPEND
     3.5 +
     3.6 +obj = src\test.obj src\vga.obj src\timer.obj src\mouse.obj \
     3.7 +	  src\mingl.obj src\mglrast.obj src\mglgen.obj \
     3.8 +	  src\texture.obj src\palman.obj
     3.9 +bin = dos3d.exe
    3.10 +
    3.11 +CC = bcc
    3.12 +
    3.13 +# 286 instructions, large memory model
    3.14 +CFLAGS = -1 -f287 -ml -O -G -Isrc
    3.15 +
    3.16 +$(bin): $(obj)
    3.17 +	$(CC) @&&|
    3.18 +$(CFLAGS) -e$@
    3.19 +$(obj)
    3.20 +|
    3.21 +
    3.22 +.SUFFIXES: .c .obj
    3.23 +
    3.24 +.c.obj:
    3.25 +	$(CC) $(CFLAGS) -o$@ -c $<
    3.26 +
    3.27 +clean:
    3.28 +	del src\*.obj
    3.29 +	del $(bin)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/firstp/firstp.c	Tue Nov 29 07:22:49 2011 +0200
     4.3 @@ -0,0 +1,251 @@
     4.4 +#include <stdio.h>
     4.5 +#include <stdlib.h>
     4.6 +#include <math.h>
     4.7 +#include <signal.h>
     4.8 +#include <conio.h>
     4.9 +#include "vga.h"
    4.10 +#include "mingl.h"
    4.11 +#include "timer.h"
    4.12 +#include "mouse.h"
    4.13 +#include "texture.h"
    4.14 +#include "palman.h"
    4.15 +#include "scene.h"
    4.16 +
    4.17 +#define DEG2RAD(x)	(M_PI * (x) / 180.0)
    4.18 +
    4.19 +static int init(void);
    4.20 +static void shutdown(void);
    4.21 +static void redraw(void);
    4.22 +static int proc_events(void);
    4.23 +static int keyb(int key);
    4.24 +static void mouse_button(int bn, int x, int y);
    4.25 +static void mouse_motion(int x, int y);
    4.26 +static void sighandler(int s);
    4.27 +
    4.28 +
    4.29 +static float cam_x, cam_y, cam_z = 10;
    4.30 +static float cam_theta, cam_phi;
    4.31 +
    4.32 +static float walk_speed = 0.1;
    4.33 +static float look_speed = 1.0;
    4.34 +static int mouse_look = 0;
    4.35 +
    4.36 +static void *fbuf;
    4.37 +static struct scene scn;
    4.38 +
    4.39 +
    4.40 +int main(void)
    4.41 +{
    4.42 +	if(init() == -1) {
    4.43 +		return 1;
    4.44 +	}
    4.45 +
    4.46 +	while(proc_events()) {
    4.47 +		redraw();
    4.48 +	}
    4.49 +
    4.50 +	shutdown();
    4.51 +	return 0;
    4.52 +}
    4.53 +
    4.54 +
    4.55 +static int init(void)
    4.56 +{
    4.57 +	init_timer(100);
    4.58 +
    4.59 +	set_video_mode(0x13);
    4.60 +
    4.61 +	signal(SIGINT, sighandler);
    4.62 +	signal(SIGSEGV, sighandler);
    4.63 +	signal(SIGFPE, sighandler);
    4.64 +	signal(SIGILL, sighandler);
    4.65 +	signal(SIGABRT, sighandler);
    4.66 +
    4.67 +	if(mgl_init(320, 200) == -1) {
    4.68 +		fprintf(stderr, "mgl init failed\n");
    4.69 +		return -1;
    4.70 +	}
    4.71 +	fbuf = mgl_framebuffer();
    4.72 +
    4.73 +	mgl_enable(MGL_CULL_FACE);
    4.74 +	mgl_enable(MGL_SMOOTH);
    4.75 +	mgl_enable(MGL_LIGHTING);
    4.76 +	mgl_enable(MGL_DEPTH_TEST);
    4.77 +
    4.78 +	mgl_matrix_mode(MGL_PROJECTION);
    4.79 +	mgl_load_identity();
    4.80 +	mgl_perspective(45.0, 320.0 / 200.0, 0.5, 200.0);
    4.81 +
    4.82 +	/* setup palette */
    4.83 +	palm_add_color(255, 255, 255);
    4.84 +
    4.85 +	scn_init(&scn);
    4.86 +	if(scn_load(&scn, "data/hall.obj") == -1) {
    4.87 +		return -1;
    4.88 +	}
    4.89 +
    4.90 +	palm_build();
    4.91 +	{
    4.92 +		int i, palsz = palm_palette_size();
    4.93 +		struct palm_color *pal = palm_palette();
    4.94 +
    4.95 +		for(i=0; i<palsz; i++) {
    4.96 +			set_palette(i, pal[i].r, pal[i].g, pal[i].b);
    4.97 +		}
    4.98 +	}
    4.99 +
   4.100 +	mgl_color_range(palm_color_range() - 1);
   4.101 +	return 0;
   4.102 +}
   4.103 +
   4.104 +static void shutdown(void)
   4.105 +{
   4.106 +	mgl_free();
   4.107 +	set_video_mode(3);
   4.108 +}
   4.109 +
   4.110 +static void redraw(void)
   4.111 +{
   4.112 +	mgl_clear(0);
   4.113 +	mgl_clear_depth();
   4.114 +
   4.115 +	mgl_matrix_mode(MGL_MODELVIEW);
   4.116 +	mgl_load_identity();
   4.117 +	mgl_rotate(cam_theta, 0, 1, 0);
   4.118 +	mgl_rotate(cam_phi, 1, 0, 0);
   4.119 +	mgl_translate(-cam_x, -cam_y, -cam_z);
   4.120 +
   4.121 +	mgl_light_intensity(0, 1.0);
   4.122 +	mgl_light_position(0, 0, 5, 0, 1);
   4.123 +
   4.124 +	/*mgl_torus(0.75, 0.25, 16, 8);*/
   4.125 +	scn_render(&scn);
   4.126 +
   4.127 +	copy_frame(fbuf);
   4.128 +}
   4.129 +
   4.130 +static int proc_events(void)
   4.131 +{
   4.132 +	static int prev_mx, prev_my, prev_bnmask;
   4.133 +	int mx, my, bnmask;
   4.134 +
   4.135 +	if(kbhit()) {
   4.136 +		if(keyb(getch()) == 0) {
   4.137 +			return 0;
   4.138 +		}
   4.139 +	}
   4.140 +
   4.141 +	bnmask = read_mouse(&mx, &my);
   4.142 +	if(bnmask != prev_bnmask) {
   4.143 +		mouse_button(bnmask, mx, my);
   4.144 +		prev_bnmask = bnmask;
   4.145 +	}
   4.146 +	if(mx != prev_mx || my != prev_my) {
   4.147 +		mouse_motion(mx, my);
   4.148 +		prev_mx = mx;
   4.149 +		prev_my = my;
   4.150 +	}
   4.151 +	return 1;
   4.152 +}
   4.153 +
   4.154 +static int keyb(int key)
   4.155 +{
   4.156 +	float dir_x, dir_y, right_x, right_y;
   4.157 +
   4.158 +	dir_x = sin(DEG2RAD(cam_theta)) * walk_speed;
   4.159 +	dir_y = cos(DEG2RAD(cam_theta)) * walk_speed;
   4.160 +	right_x = dir_y;
   4.161 +	right_y = -dir_x;
   4.162 +
   4.163 +	switch(key) {
   4.164 +	case 27:
   4.165 +		return 0;
   4.166 +
   4.167 +	case 'w':
   4.168 +		cam_x += dir_x;
   4.169 +		cam_z -= dir_y;
   4.170 +		break;
   4.171 +
   4.172 +	case 's':
   4.173 +		cam_x -= dir_x;
   4.174 +		cam_z += dir_y;
   4.175 +		break;
   4.176 +
   4.177 +	case 'a':
   4.178 +		cam_x -= right_x;
   4.179 +		cam_z += right_y;
   4.180 +		break;
   4.181 +
   4.182 +	case 'd':
   4.183 +		cam_x += right_x;
   4.184 +		cam_z -= right_y;
   4.185 +		break;
   4.186 +
   4.187 +	case '`':
   4.188 +		mouse_look = !mouse_look;
   4.189 +		break;
   4.190 +
   4.191 +	default:
   4.192 +		break;
   4.193 +	}
   4.194 +	return 1;
   4.195 +}
   4.196 +
   4.197 +static int bnstate;
   4.198 +static void mouse_button(int bn, int x, int y)
   4.199 +{
   4.200 +	bnstate = bn;
   4.201 +}
   4.202 +
   4.203 +static void mouse_motion(int x, int y)
   4.204 +{
   4.205 +	static int prev_x, prev_y;
   4.206 +	int dx = x - prev_x;
   4.207 +	int dy = y - prev_y;
   4.208 +	prev_x = x;
   4.209 +	prev_y = y;
   4.210 +
   4.211 +	if(mouse_look || bnstate) {
   4.212 +		cam_theta += dx * look_speed;
   4.213 +		cam_phi += dy * look_speed;
   4.214 +
   4.215 +		if(cam_phi < -90) {
   4.216 +			cam_phi = -90;
   4.217 +		}
   4.218 +		if(cam_phi > 90) {
   4.219 +			cam_phi = 90;
   4.220 +		}
   4.221 +	}
   4.222 +}
   4.223 +
   4.224 +static void sighandler(int s)
   4.225 +{
   4.226 +	set_video_mode(3);
   4.227 +
   4.228 +	switch(s) {
   4.229 +	case SIGABRT:
   4.230 +		fprintf(stderr, "abort\n");
   4.231 +		break;
   4.232 +
   4.233 +	case SIGILL:
   4.234 +		fprintf(stderr, "illegal operation\n");
   4.235 +		break;
   4.236 +
   4.237 +	case SIGSEGV:
   4.238 +		fprintf(stderr, "segmentation fault\n");
   4.239 +		break;
   4.240 +
   4.241 +	case SIGINT:
   4.242 +		fprintf(stderr, "interrupted\n");
   4.243 +		break;
   4.244 +
   4.245 +	case SIGFPE:
   4.246 +		fprintf(stderr, "floating point exception\n");
   4.247 +		break;
   4.248 +
   4.249 +	default:
   4.250 +		fprintf(stderr, "unexpected signal\n");
   4.251 +	}
   4.252 +
   4.253 +	exit(1);
   4.254 +}
     5.1 --- a/src/mglimpl.h	Mon Nov 28 05:12:33 2011 +0200
     5.2 +++ b/src/mglimpl.h	Tue Nov 29 07:22:49 2011 +0200
     5.3 @@ -18,6 +18,8 @@
     5.4  #ifndef MGL_IMPL_H_
     5.5  #define MGL_IMPL_H_
     5.6  
     5.7 +#include "vmath.h"
     5.8 +
     5.9  #define MATRIX_STACK_SIZE	8
    5.10  #define MAX_LIGHTS			4
    5.11  
    5.12 @@ -30,17 +32,6 @@
    5.13  
    5.14  #define ROUND(x)	((x) >= 0.0 ? (x) + 0.5 : (x) - 0.5)
    5.15  
    5.16 -typedef struct {
    5.17 -	float x, y, z, w;
    5.18 -} vec4_t;
    5.19 -
    5.20 -typedef struct {
    5.21 -	float x, y, z;
    5.22 -} vec3_t;
    5.23 -
    5.24 -typedef struct {
    5.25 -	float x, y;
    5.26 -} vec2_t;
    5.27  
    5.28  typedef float mat4_t[16];
    5.29