block placing
This commit is contained in:
parent
b4a7ec7cc3
commit
8ac68e31e7
@ -39,8 +39,11 @@ void app_load_textures(App *app);
|
|||||||
GLuint app_get_texture(App *app, Block_type type);
|
GLuint app_get_texture(App *app, Block_type type);
|
||||||
|
|
||||||
int check_intersection_block(App *app, vec3i *retv, int *chunk_index);
|
int check_intersection_block(App *app, vec3i *retv, int *chunk_index);
|
||||||
|
|
||||||
void app_break_block(App *app);
|
void app_break_block(App *app);
|
||||||
|
|
||||||
|
void app_place_block(App *app);
|
||||||
|
|
||||||
bool app_check_collision(vec3f camera_pos, ChunkManager *cm);
|
bool app_check_collision(vec3f camera_pos, ChunkManager *cm);
|
||||||
|
|
||||||
void init_app(App *app, uint w, uint h);
|
void init_app(App *app, uint w, uint h);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
|
|
||||||
#define GRAVITY 1
|
#define GRAVITY 5
|
||||||
/**
|
/**
|
||||||
* Camera, as a moving point with direction
|
* Camera, as a moving point with direction
|
||||||
*/
|
*/
|
||||||
|
76
src/app.c
76
src/app.c
@ -57,7 +57,7 @@ void app_load_textures(App *app)
|
|||||||
app->surfaces[1].surface = IMG_Load("assets/block.png");
|
app->surfaces[1].surface = IMG_Load("assets/block.png");
|
||||||
app->surfaces[2].surface = IMG_Load("assets/block.png");
|
app->surfaces[2].surface = IMG_Load("assets/block.png");
|
||||||
|
|
||||||
for (int i = 0; i < app->surface_count-2; i++) {
|
for (int i = 0; i < app->surface_count-1; i++) {
|
||||||
if(app->surfaces[i].surface == NULL) {
|
if(app->surfaces[i].surface == NULL) {
|
||||||
printf("error loading texture\n");
|
printf("error loading texture\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -78,15 +78,12 @@ void app_load_textures(App *app)
|
|||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, app->surfaces[i].tid);
|
//glBindTexture(GL_TEXTURE_2D, app->surfaces[i].tid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void update_app(App* app)
|
void update_app(App* app)
|
||||||
{
|
{
|
||||||
|
|
||||||
update_camera(&(app->camera), app->frame_time/1000);
|
update_camera(&(app->camera), app->frame_time/1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,8 +191,13 @@ void handle_events(App* app)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
app_break_block(app);
|
if (event.button.button == SDL_BUTTON_LEFT) {
|
||||||
break;
|
app_break_block(app);
|
||||||
|
break;
|
||||||
|
} else if (event.button.button == SDL_BUTTON_RIGHT) {
|
||||||
|
app_place_block(app);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
SDL_GetMouseState(&x, &y);
|
SDL_GetMouseState(&x, &y);
|
||||||
rotate_camera(&(app->camera), mouse_x - x, mouse_y - y);
|
rotate_camera(&(app->camera), mouse_x - x, mouse_y - y);
|
||||||
@ -336,7 +338,7 @@ int check_intersection_block(App *app, vec3i *retv, int *chunk_index)
|
|||||||
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
||||||
for (int z = 0; z < CHUNK_MAX_Z; z++) {
|
for (int z = 0; z < CHUNK_MAX_Z; z++) {
|
||||||
if (app->cm->chunks[i].blocks[x][y][z].visible) {
|
if (app->cm->chunks[i].blocks[x][y][z].visible) {
|
||||||
for (float j = 0; j < 8; j+=0.2) {
|
for (float j = 0; j < 5; j+=0.2) {
|
||||||
vec3f ray = {(c_pos.x - app->cm->chunks[i].start_pos.x) + c_dir.x * j,
|
vec3f ray = {(c_pos.x - app->cm->chunks[i].start_pos.x) + c_dir.x * j,
|
||||||
(c_pos.y - app->cm->chunks[i].start_pos.y) + c_dir.y * j,
|
(c_pos.y - app->cm->chunks[i].start_pos.y) + c_dir.y * j,
|
||||||
(c_pos.z - app->cm->chunks[i].start_pos.z) + c_dir.z * j};
|
(c_pos.z - app->cm->chunks[i].start_pos.z) + c_dir.z * j};
|
||||||
@ -369,10 +371,66 @@ void app_break_block(App *app)
|
|||||||
//printf("chunk %d. min x: %f y: %f max x: %f y: %f\n", i, app->chunks[i].start_pos.x,
|
//printf("chunk %d. min x: %f y: %f max x: %f y: %f\n", i, app->chunks[i].start_pos.x,
|
||||||
// app->chunks[i].start_pos.y, app->chunks[i].start_pos.x+CHUNK_MAX_X, app->chunks[i].start_pos.y+CHUNK_MAX_Y);
|
// app->chunks[i].start_pos.y, app->chunks[i].start_pos.x+CHUNK_MAX_X, app->chunks[i].start_pos.y+CHUNK_MAX_Y);
|
||||||
//printf("broken block x: %f y: %f z: %f\n", block.x+app->chunks[i].start_pos.x, block.y+app->chunks[i].start_pos.y, block.z+app->chunks[i].start_pos.z);
|
//printf("broken block x: %f y: %f z: %f\n", block.x+app->chunks[i].start_pos.x, block.y+app->chunks[i].start_pos.y, block.z+app->chunks[i].start_pos.z);
|
||||||
if(ret < 0)
|
if (ret < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
app->cm->chunks[i].blocks[block.x][block.y][block.z].type = BLOCKTYPE_AIR;
|
app->cm->chunks[i].blocks[block.x][block.y][block.z].type = BLOCKTYPE_AIR;
|
||||||
chunk_update(app->cm, &(app->cm->chunks[i]));
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void app_place_block(App *app)
|
||||||
|
{
|
||||||
|
float dist = 5.0;
|
||||||
|
vec3f cam_pos = app->camera.position;
|
||||||
|
vec3i block;
|
||||||
|
int i;
|
||||||
|
int ret = check_intersection_block(app, &block, &i);
|
||||||
|
|
||||||
|
if (ret < 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
vec3f t = { (float) cam_pos.x - block.x,
|
||||||
|
(float) cam_pos.y - block.y,
|
||||||
|
(float) cam_pos.z - block.z };
|
||||||
|
if (t.x > 0) {
|
||||||
|
if (t.y < t.x && t.y > -t.x) {
|
||||||
|
app->cm->chunks[i].blocks[block.x+1][block.y][block.z].type = BLOCKTYPE_STONE;
|
||||||
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (t.y < -t.x && t.y > t.x) {
|
||||||
|
app->cm->chunks[i].blocks[block.x-1][block.y][block.z].type = BLOCKTYPE_STONE;
|
||||||
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t.y > 0) {
|
||||||
|
app->cm->chunks[i].blocks[block.x][block.y+1][block.z].type = BLOCKTYPE_STONE;
|
||||||
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
app->cm->chunks[i].blocks[block.x][block.y-1][block.z].type = BLOCKTYPE_STONE;
|
||||||
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//if (cam_pos.x < (float)block.x+dist/2 &&
|
||||||
|
// cam_pos.x > (float)block.x-dist/2) {
|
||||||
|
// if (cam_pos.x > block.x)
|
||||||
|
// app->cm->chunks[i].blocks[block.x+1][block.y][block.z].type = BLOCKTYPE_STONE;
|
||||||
|
// if (cam_pos.x < block.x)
|
||||||
|
// app->cm->chunks[i].blocks[block.x-1][block.y][block.z].type = BLOCKTYPE_STONE;
|
||||||
|
// chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
//}
|
||||||
|
//if (cam_pos.y < (float)block.y+dist/2 &&
|
||||||
|
// cam_pos.y > (float)block.y-dist/2) {
|
||||||
|
// if (cam_pos.y > block.y)
|
||||||
|
// app->cm->chunks[i].blocks[block.x][block.y+1][block.z].type = BLOCKTYPE_STONE;
|
||||||
|
// if (cam_pos.y < block.y)
|
||||||
|
// app->cm->chunks[i].blocks[block.x][block.y-1][block.z].type = BLOCKTYPE_STONE;
|
||||||
|
// chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ void draw_cube(ChunkManager *cm, Block_type type, float x, float y, float z)
|
|||||||
void chunk_create_displayl(ChunkManager *cm, Chunk *chunk)
|
void chunk_create_displayl(ChunkManager *cm, Chunk *chunk)
|
||||||
{
|
{
|
||||||
chunk->id = glGenLists(1);
|
chunk->id = glGenLists(1);
|
||||||
glBindTexture(GL_TEXTURE_2D, cm->textures[0].tid);
|
//glBindTexture(GL_TEXTURE_2D, cm->textures[0].tid);
|
||||||
glNewList(chunk->id, GL_COMPILE);
|
glNewList(chunk->id, GL_COMPILE);
|
||||||
for (int x = 0; x < CHUNK_MAX_X; x++) {
|
for (int x = 0; x < CHUNK_MAX_X; x++) {
|
||||||
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user