Skip to content

Commit

Permalink
Add a scrollable 9-item hotbar, progress for GH-18 inventory (#177, #175
Browse files Browse the repository at this point in the history
)

* Cherry-pick from "HUD expansion": twetzel59/CraftNG@a604e73

* Decrease item rendering size to 32

* Rename _item to item_hotbar to clarify its purpose

* Revert "Rename _item to item_hotbar to clarify its purpose"

This reverts commit 59f7056.

* Cherry-pick item counts from "Inventory with HUD."

twetzel59/CraftNG@3b44099
twetzel59/CraftNG@7e7c624

* Refactor render_item_count tx/ty

* Move the hotbar flush up against the right edge, fix width shifting

#175 (comment)

* Translate Z of item hotbar slightly to fix text overlap

* Increase clipping plane and z translate to not overlap on web

* Fix unnecessary whitespace after item count text

* Zero-initialize fixed size (not variable) buf in render_item_count

* Increase hotbar size to 9 items

* Remove count labels from hotbar items for now
  • Loading branch information
satoshinm authored Jun 4, 2017
1 parent 2cd2030 commit 0d489b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
54 changes: 43 additions & 11 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1875,16 +1875,30 @@ void render_item(Attrib *attrib) {
glUniform3f(attrib->camera, 0, 0, 5);
glUniform1i(attrib->sampler, 0);
glUniform1f(attrib->timer, time_of_day());
int w = hotbar_items[g->item_index];
if (is_plant(w)) {
GLuint buffer = gen_plant_buffer(0, 0, 0, 0.5, w);
draw_plant(attrib, buffer);
del_buffer(buffer);
}
else {
GLuint buffer = gen_cube_buffer(0, 0, 0, 0.5, w);
draw_cube(attrib, buffer);
del_buffer(buffer);

for (int i = 0; i < 9; ++i) {
if (g->item_index + i >= hotbar_item_count) {
break;
}

glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix);

int w = hotbar_items[g->item_index + i];
if (is_plant(w)) {
GLuint buffer = gen_plant_buffer(0, 0, 0, 0.5, w);
draw_plant(attrib, buffer);
del_buffer(buffer);
} else {
GLuint buffer = gen_cube_buffer(0, 0, 0, 0.5, w);
draw_cube(attrib, buffer);
del_buffer(buffer);
}

if (!i) {
set_matrix_item(matrix, g->width, g->height, g->scale);
}

matrix[13] += 0.2126;
}
}

Expand All @@ -1904,6 +1918,23 @@ void render_text(
del_buffer(buffer);
}

void render_item_count(Attrib *attrib, float ts) {
float ty = 15.0f;
for (int i = 0; i < 9; ++i) {
if (g->item_index + i >= hotbar_item_count) {
break;
}

char buf[4] = {0};
//snprintf(buf, sizeof(buf), "%d", 16); // TODO: finite inventory
float tx = g->width - 20.0f;
render_text(attrib, ALIGN_CENTER, tx, ty, ts, buf);

float ratio_to_hardcoded = (g->height / 768.0f);
ty += 81.705 * ratio_to_hardcoded;
}
}

void add_message(const char *text) {
printf("%s\n", text);
snprintf(
Expand Down Expand Up @@ -3397,6 +3428,7 @@ void render_scene() {
// RENDER 3-D SCENE //
render_sky(&sky_attrib, player, sky_buffer);
int face_count;
float ts = 12 * g->scale;
if (g->initialized) {
glClear(GL_DEPTH_BUFFER_BIT);
face_count = render_chunks(&block_attrib, player);
Expand All @@ -3418,14 +3450,14 @@ void render_scene() {
}
if (SHOW_ITEM && g->show_ui) {
render_item(&block_attrib);
render_item_count(&text_attrib, ts);
}
} else {
face_count = 0;
}

// RENDER TEXT //
char text_buffer[1024];
float ts = 12 * g->scale;
float tx = ts / 2;
float ty = g->height - ts;
if (g->show_info_text && g->show_ui && g->initialized) {
Expand Down
6 changes: 3 additions & 3 deletions src/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void set_matrix_item(float *matrix, int width, int height, int scale) {
float a[16];
float b[16];
float aspect = (float)width / height;
float size = 64 * scale;
float size = 32 * scale;
float box = height / size / 2;
float xoffset = 1 - size / width * 2;
float yoffset = 1 - size / height * 2;
Expand All @@ -250,9 +250,9 @@ void set_matrix_item(float *matrix, int width, int height, int scale) {
mat_multiply(a, b, a);
mat_rotate(b, 1, 0, 0, -PI / 10);
mat_multiply(a, b, a);
mat_ortho(b, -box * aspect, box * aspect, -box, box, -1, 1);
mat_ortho(b, -box * aspect, box * aspect, -box, box, -1, 2);
mat_multiply(a, b, a);
mat_translate(b, -xoffset, -yoffset, 0);
mat_translate(b, xoffset, -yoffset, 1.0f);
mat_multiply(a, b, a);
mat_identity(matrix);
mat_multiply(matrix, a, matrix);
Expand Down

0 comments on commit 0d489b9

Please sign in to comment.