polyadvent

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

scene.c (2512B)


      1 
      2 
      3 #include "game.h"
      4 #include "model.h"
      5 #include "procmesh.h"
      6 #include "debug.h"
      7 #include "vec3.h"
      8 #include <assert.h>
      9 
     10 void reset_scene(struct game *game) {
     11     struct entity *terrain_ent = get_terrain_entity(&game->terrain);
     12     struct entity *player_ent = get_player(&game->test_resources);
     13 
     14     destroy_entities();
     15 
     16     // hide terrain and player by default
     17     terrain_ent->flags |= ENT_INVISIBLE;
     18     player_ent->flags |= ENT_INVISIBLE;
     19 }
     20 
     21 void default_scene(struct game *game) {
     22     struct terrain *terrain = &game->terrain;
     23     struct entity *terrain_ent = get_terrain_entity(terrain);
     24     struct entity *player = get_player(&game->test_resources);
     25 
     26     // show terrain
     27     terrain_ent->flags &= ~ENT_INVISIBLE;
     28 
     29     // show player
     30     player->flags &= ~ENT_INVISIBLE;
     31 
     32     struct entity *tower = new_entity(NULL);
     33     struct node *tnode = get_node(&tower->node_id);
     34 
     35     assert(tnode);
     36     tower->model_id = get_static_model(model_tower, NULL);
     37     node_set_label(tnode, "tower");
     38     node_attach(&tower->node_id, &player->node_id);
     39     node_translate(tnode, V3(0.0, 50.0, 0.0));
     40     node_recalc(tnode);
     41     float z = terrain->fn(terrain, tnode->mat[M_X], tnode->mat[M_Y]);
     42     /* node_detach(tnode, pnode); */
     43     tnode->mat[M_Z] = z;
     44     // END ENTITIES
     45 
     46 
     47 }
     48 
     49 
     50 
     51 void entity_test_scene(struct game *game)
     52 {
     53     struct terrain *terrain  = &game->terrain;
     54 
     55     model_id rock_model;
     56     init_id(&rock_model);
     57 
     58     /* model_id rock_model = get_static_model(model_tower, NULL); */
     59     struct model *pmodel  = new_model(&rock_model); assert(pmodel);
     60     struct geometry *geom = get_geometry(&pmodel->geom_id); assert(geom);
     61     proc_sphere(geom);
     62 
     63     for (int i = 0; i < 200; i++) {
     64         struct entity *ent = new_entity(NULL);
     65         struct node *node  = get_node(&ent->node_id);
     66 
     67         ent->model_id = rock_model;
     68 
     69         double x = rand_0to1() * terrain->size;
     70         double y = rand_0to1() * terrain->size;
     71         double z = terrain->fn(terrain, x, y);
     72 
     73         node_scale(node, pow(15.0, rand_0to1()));
     74         node_rotate(node, V3(rand_0to1(),rand_0to1(),rand_0to1()));
     75         node_translate(node, V3(x, y, z));
     76         node_set_label(node, "rock");
     77 
     78         node_recalc(node);
     79     }
     80 
     81 }
     82 
     83 void pbr_scene(struct game *game)
     84 {
     85     struct entity *ent = new_entity(NULL);
     86     struct node *node  = get_node(&ent->node_id); assert(node);
     87     struct entity *player = get_player(&game->test_resources);
     88 
     89     ent->model_id = get_static_model(model_icosphere, NULL);
     90     node_set_label(node, "sphere");
     91 }