polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

chess.c (4079B)


      1 
      2 #include "chess.h"
      3 #include "node.h"
      4 #include "gpu.h"
      5 #include "shader.h"
      6 #include "hash.h"
      7 #include "grid.h"
      8 
      9 struct chess_piece {
     10 	int is_white;
     11 };
     12 
     13 #define CP_LENS(field, typ) { #field, offsetof(struct chess_piece, field), DATA_ID_##typ }
     14 static struct lens chess_piece_lenses[] = {
     15 	CP_LENS(is_white, INT),
     16 };
     17 
     18 static void position_piece(struct entity *piece, int cell_size, int x, int y)
     19 {
     20 	assert(x < 8);
     21 	assert(y < 8);
     22 
     23 	struct node *node = get_node(&piece->node_id);
     24 	assert(node);
     25 
     26 	// reset to a1
     27 	node->pos[0] = -3.5 * cell_size;
     28 	node->pos[1] = -3.5 * cell_size;
     29 	node->pos[2] = 0.0;
     30 
     31 	node->pos[0] += x * cell_size;
     32 	node->pos[1] += y * cell_size;
     33 }
     34 
     35 static void setup_pieces(struct node_id *chessboard, float cell_size)
     36 {
     37 	struct model *pirate_officer, *pawn_model, *tower_model, *rook_model;
     38 	struct model_id pawn_model_id, rook_model_id;
     39 	u32 ent_type;
     40 
     41 	ent_type = hash_str("chess-piece");
     42 	debug("chess piece ent_type %d\n", ent_type);
     43 
     44 	get_model_by_name("pirate_officer", &pirate_officer);
     45 	get_model_by_name("tower", &tower_model);
     46 
     47 	// make pawn model
     48 	init_id(&pawn_model_id);
     49 	init_id(&rook_model_id);
     50 
     51 	pawn_model = new_model(&pawn_model_id, "pawn");
     52 	pawn_model->geom_id = pirate_officer->geom_id;
     53 	pawn_model->name = "pawn";
     54 
     55 	rook_model = new_model(&rook_model_id, "rook");
     56 	rook_model->geom_id = tower_model->geom_id;
     57 	rook_model->name = "rook";
     58 
     59 	// pawns
     60 	for (int i = 0; i < 16; i++) {
     61 		struct entity *ent = new_entity(NULL);
     62 		struct node *pawn_node = get_node(&ent->node_id);
     63 		struct chess_piece *pawn = (struct chess_piece*)ent->data;
     64 
     65 		ent->type = ent_type;
     66 		ent->model_id = pawn_model_id;
     67 		ent->flags |= ENT_CASTS_SHADOWS;
     68 
     69 		node_set_label(pawn_node, "pawn");
     70 
     71 		pawn->is_white = 1;
     72 
     73 		node_scale(pawn_node, 5.0);
     74 
     75 		if (i >= 8) {
     76 			pawn->is_white = 0;
     77 			node_rotate(pawn_node, V3(0.0,0.0,135.0));
     78 		}
     79 
     80 		node_attach(&ent->node_id, chessboard);
     81 
     82 		position_piece(ent, cell_size, i % 8, i < 8 ? 1 : 6);
     83 	}
     84 
     85 	for (int i = 0; i < 4; i++) {
     86 		// rooks
     87 		struct entity *ent = new_entity(NULL);
     88 		struct chess_piece *rook = (struct chess_piece*)ent->data;
     89 		struct node *rook_node = get_node(&ent->node_id);
     90 
     91 		ent->type = ent_type;
     92 		ent->model_id = rook_model_id;
     93 		ent->flags |= ENT_CASTS_SHADOWS;
     94 
     95 		rook->is_white = true;
     96 
     97 		node_attach(&ent->node_id, chessboard);
     98 		/* node_rotate(rook_node, V3(0.0,0.0,20.0)); */
     99 		if (i >= 2) {
    100 			rook->is_white = false;
    101 			node_rotate(rook_node, V3(0.0,0.0,135.0));
    102 		}
    103 		position_piece(ent, cell_size, i % 2 ? 0 : 7, i < 2 ? 0 : 7);
    104 		node_scale(rook_node, 0.25);
    105 	}
    106 
    107 
    108 }
    109 
    110 
    111 /* needs some fragment shader, pro
    112 
    113 	default_program = get_gpu_program_by_name(gpu, "vertex-color");
    114 	rtassert(default_program, "failed to get default program");
    115 	check_gl();
    116 
    117 	fragment = get_program_shader(default_program, GL_FRAGMENT_SHADER);
    118 	rtassert(fragment, "failed to get default fragment shader");
    119 	*/
    120 
    121 struct entity_id make_chessboard(struct gpu *gpu)
    122 {
    123 	static const float cell_size = 10.0;
    124 
    125 	struct entity_id chessboard_ent_id;
    126 	struct geometry_id chessboard_geom_id;
    127 	struct model_id chessboard_model_id;
    128 	struct model *chessboard_model;
    129 	struct entity *chessboard;
    130 	struct node *node;
    131 	struct geometry *geom;
    132 
    133 	init_id(&chessboard_geom_id);
    134 	init_id(&chessboard_model_id);
    135 	init_id(&chessboard_ent_id);
    136 
    137 	chessboard = new_entity(&chessboard_ent_id);
    138 	node = get_node(&chessboard->node_id);
    139 	geom = new_geometry(&chessboard_geom_id);
    140 	assert(node);
    141 
    142 	// setup chessboard
    143 	make_grid_geom(geom, 10, 10, cell_size);
    144 
    145 	chessboard_model = new_model(&chessboard_model_id, "chessboard");
    146 	chessboard_model->geom_id = chessboard_geom_id;
    147 
    148 	chessboard->model_id = chessboard_model_id;
    149 	chessboard->flags &= ~ENT_CASTS_SHADOWS;
    150 
    151 	setup_pieces(&chessboard->node_id, cell_size);
    152 
    153 	return chessboard_ent_id;
    154 }
    155 
    156 struct entity_id chess_scene(struct gpu *gpu, struct orbit *orbcam)
    157 {
    158 	struct entity_id chessboard = make_chessboard(gpu);
    159 
    160 	// setup camera
    161 	if (orbcam) {
    162 		struct spherical *coords = &orbcam->coords;
    163 		coords->radius = 72.0;
    164 		coords->inclination = 0.5;
    165 		coords->azimuth = -7.86;
    166 	}
    167 
    168 	return chessboard;
    169 }
    170