So all my entities have a ibo_id when they're push'd or created during game initialization.
The ibo_id is the first of 6 indices in the ibo which, as far as i've understood the indexbuffer decides which quad will be drawn first.
so these are the indices of the first quad (triangle-pair)
ibo[0] = 0;
ibo[1] = 1;
ibo[2] = 2;
ibo[3] = 0;
ibo[4] = 2;
ibo[5] = 3;
and so on....
what my swap function does is:
1. check if entity_1.pos.y is less then entity_2.pos.y
2. If it is, check if ibo[entity_1.ibo_id] is less than ibo[entity_2.ibo_id].
3 If it is, swap the 6 values corresponding to the entities ibo_id's
The thing is that if I only use the function on 2 entites like SwapOrder(game->player, game->monster);
the player is draw ontop of the monster when it goes below the monster and vice verse.
While if I first check the player against all tiles and then against the monster, the player is not drawn ontop of the monster...
I've checked the values of the index buffer and they seem fine meaning that the player's ibo_values are greater than the monsters
in both cases.
Im starting to wonder if the indexbuffer is not doing what i assume it does, that is, draw the last indices ontop of the first indices??
Code-snippets attached:
1. PushEntity()
2. SwapOrderInIBO()
3. Full main.cpp using SwapOrderInIBO() on lines ( 549 - 667 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ENTITY *PushEntity(GAME_HANDLER *game, u32 size) { assert(game->entity_used + size <= game->entity_batch_count); ENTITY *entity = 0; CJ_VTX_QUAD *vtx = 0; entity = game->entity_batch + game->entity_used;; entity->id = game->entity_id; entity->ibo_id = game->ibo_id; vtx = game->pVertex + game->vtx_id; // vtx_id is for pushing 4 * CJ_VTX_QUAD game->vtx_id += 4 * size; game->ibo_id += 6 * size; game->entity_used += size; game->entity_id++; return entity; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | void SwapOrderInIBO(CJ_RENDERER *renderer, ENTITY *e1, ENTITY *e2) { if(e1->pos.y < e2->pos.y) { u32 e1_ibo_index = e1->ibo_id; u32 e2_ibo_index = e2->ibo_id; u32 e1_ibo_value = renderer->ibo_sprite.base[e1_ibo_index]; u32 e2_ibo_value = renderer->ibo_sprite.base[e2_ibo_index]; if(e1_ibo_value < e2_ibo_value) { for(u32 i = 0; i < 6; i++) { e1_ibo_value = renderer->ibo_sprite.base[e1_ibo_index + i]; e2_ibo_value = renderer->ibo_sprite.base[e2_ibo_index + i]; u32 temp_ibo_value = e1_ibo_value; renderer->ibo_sprite.base[e1_ibo_index + i] = e2_ibo_value; renderer->ibo_sprite.base[e2_ibo_index + i] = temp_ibo_value; } } } else if(e1->pos.y > e2->pos.y) { u32 e1_ibo_index = e1->ibo_id; u32 e2_ibo_index = e2->ibo_id; u32 e1_ibo_value = renderer->ibo_sprite.base[e1_ibo_index]; u32 e2_ibo_value = renderer->ibo_sprite.base[e2_ibo_index]; if(e1_ibo_value > e2_ibo_value) { for(u32 i = 0; i < 6; i++) { e1_ibo_value = renderer->ibo_sprite.base[e1_ibo_index + i]; e2_ibo_value = renderer->ibo_sprite.base[e2_ibo_index + i]; u32 temp_ibo_value = e1_ibo_value; renderer->ibo_sprite.base[e1_ibo_index + i] = e2_ibo_value; renderer->ibo_sprite.base[e2_ibo_index + i] = temp_ibo_value; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 | bool running = true; #include "assert.h" #include "cj_engine.h" GLenum err = 0; struct TEXT_INFO { V2f sentence_pos; V2f sentence_size; u32 n_files_in_dir; }; struct TILEMAP { u32 w; u32 h; ENTITY *entity; }; struct GAME_HANDLER { ENTITY *entity_batch; u32 entity_batch_count; u32 entity_used; u32 entity_id; CJ_VTX_QUAD *pVertex; u32 vtx_id; u32 ibo_id; TILEMAP tilemap; ENTITY *player; ENTITY *monster; ENTITY *current_chosen_atlas; ENTITY *camera; }; bool CursorInsideText(CJ_PLATFORM platform, TEXT_INFO ti); TEXT_INFO DrawText(CJ_PLATFORM *platform, CJ_RENDERER *renderer, u32 texture_index, char *str, V2f pos, V4f col); void SwapOrderInIBO(CJ_RENDERER *renderer, ENTITY *e1, ENTITY *e2); void GetDirContent(TEXT_INFO *ti, char *storage, char *dir, u32 max_filesz_name) { ti->n_files_in_dir = 0; WIN32_FIND_DATA find_data = {}; LARGE_INTEGER file_sz = {}; HANDLE hFind = {}; strcpy(storage, dir); strcat(storage, "/*"); ti->n_files_in_dir++; hFind = FindFirstFile(storage, &find_data); if(hFind == INVALID_HANDLE_VALUE) printf("couldn't find file in %s\n", storage); else storage += max_filesz_name; while(FindNextFile(hFind, &find_data) != 0) { if(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { strcpy(storage, find_data.cFileName); storage += max_filesz_name; ti->n_files_in_dir++; } else { char *dir_assets = "assets/"; strcpy(storage, dir_assets); strcat(storage, find_data.cFileName); storage += max_filesz_name; ti->n_files_in_dir++; } } } ENTITY *PushEntity(GAME_HANDLER *game, u32 size) { assert(game->entity_used + size <= game->entity_batch_count); ENTITY *entity = 0; CJ_VTX_QUAD *vtx = 0; entity = game->entity_batch + game->entity_used;; entity->id = game->entity_id; entity->ibo_id = game->ibo_id; vtx = game->pVertex + game->vtx_id; // vtx_id is for pushing 4 * CJ_VTX_QUAD game->vtx_id += 4 * size; game->ibo_id += 6 * size; game->entity_used += size; game->entity_id++; return entity; } void UpdateVBO_Entity(CJ_VBO *vbo, GAME_HANDLER *game) { CJ_VTX_QUAD *pVertex = (CJ_VTX_QUAD*)vbo->base; assert(game->entity_used <= game->entity_batch_count); for(u32 i = 0; i < game->entity_used; i++) { V2f pos = game->entity_batch[i].pos; V2f size = game->entity_batch[i].size; V4f col = game->entity_batch[i].col; V2f tCoord_p = game->entity_batch[i].texcoord_pos; V2f tCoord_sz = game->entity_batch[i].texcoord_size; pVertex[(i * 4) + 0].pos = v2f(pos.x, pos.y); pVertex[(i * 4) + 1].pos = v2f(pos.x, pos.y + size.y); pVertex[(i * 4) + 2].pos = v2f(pos.x + size.x, pos.y + size.y); pVertex[(i * 4) + 3].pos = v2f(pos.x + size.x, pos.y); pVertex[(i * 4) + 0].col = col; pVertex[(i * 4) + 1].col = col; pVertex[(i * 4) + 2].col = col; pVertex[(i * 4) + 3].col = col; pVertex[(i * 4) + 0].tCoord = v2f(tCoord_p.x, tCoord_p.y); pVertex[(i * 4) + 1].tCoord = v2f(tCoord_p.x, tCoord_p.y + tCoord_sz.y); pVertex[(i * 4) + 2].tCoord = v2f(tCoord_p.x + tCoord_sz.x, tCoord_p.y + tCoord_sz.y); pVertex[(i * 4) + 3].tCoord = v2f(tCoord_p.x + tCoord_sz.y, tCoord_p.y); } } bool CursorInsideText(CJ_PLATFORM platform, TEXT_INFO ti) { if(platform.cursor_pos.x > ti.sentence_pos.x && platform.cursor_pos.x < ti.sentence_size.x && platform.cursor_pos.y > ti.sentence_pos.y && platform.cursor_pos.y < ti.sentence_size.y) return true; else return false; } TEXT_INFO DrawText(CJ_PLATFORM *platform, CJ_RENDERER *renderer, u32 texture_index, char *str, V2f pos, V4f col) { TEXT_INFO ti = {}; char line[1024] = {}; strcpy(line, str); // xp & yp is baseline in stbtt_GetPackedQuad(); float yp = 0.0f; float xp = 0.0f; float t_pos_x = 0.0f; float t_pos_y = 0.0f; float t_sz_x = 0.0f; float t_sz_y = 0.0f; float c_x = 0.0f; float c_y = 0.0f; float c_w = 0.0f; float c_h = 0.0f; float bigges_h = 0.0f; u32 n_quads_written = 0; glBindVertexArray(renderer->VAO_font); UseShaderProgram(&renderer->shader[2]); glBindBuffer(GL_ARRAY_BUFFER, renderer->vbo_text.id); glBindTexture(GL_TEXTURE_2D, renderer->texture_batch[texture_index].texture_id); CJ_VTX_QUAD *advPointer = (CJ_VTX_QUAD*)renderer->vbo_text.base; TEXTUREINFO *tex_i = &renderer->texture_batch[texture_index]; for(i32 i = 0; i < strlen(line); i++) { stbtt_GetPackedQuad(tex_i->packed_char, 1024, 1024, line[i] - ' ', &xp, &yp, &tex_i->glyph_quad, 0); t_pos_x = tex_i->glyph_quad.s0; t_pos_y = 1.0f - tex_i->glyph_quad.t1; t_sz_x = tex_i->glyph_quad.s1 - tex_i->glyph_quad.s0; t_sz_y = (1.0f - tex_i->glyph_quad.t0) - (1.0f - tex_i->glyph_quad.t1); c_x = pos.x + (tex_i->glyph_quad.x0) / 1080.0f; c_y = pos.y + ((yp + (yp - tex_i->glyph_quad.y1))) / 960.0f; c_w = (tex_i->glyph_quad.x1 - tex_i->glyph_quad.x0) / 1080.0f; c_h = (tex_i->glyph_quad.y1 - tex_i->glyph_quad.y0) / 960.0f; CJ_VTX_QUAD vertices[6] = {}; vertices[0].pos = v2f(c_x, c_y); vertices[0].col = v4f(col.x, col.y, col.z, col.w); vertices[0].tCoord = v2f(t_pos_x, t_pos_y); vertices[1].pos = v2f(c_x, c_y + c_h); vertices[1].col = v4f(col.x, col.y, col.z, col.w); vertices[1].tCoord = v2f(t_pos_x, t_pos_y + t_sz_y); vertices[2].pos = v2f(c_x + c_w, c_y + c_h); vertices[2].col = v4f(col.x, col.y, col.z, col.w); vertices[2].tCoord = v2f(t_pos_x + t_sz_x, t_pos_y + t_sz_y); vertices[3].pos = v2f(c_x, c_y); vertices[3].col = v4f(col.x, col.y, col.z, col.w); vertices[3].tCoord = v2f(t_pos_x, t_pos_y); vertices[4].pos = v2f(c_x + c_w, c_y + c_h); vertices[4].col = v4f(col.x, col.y, col.z, col.w); vertices[4].tCoord = v2f(t_pos_x + t_sz_x, t_pos_y + t_sz_y); vertices[5].pos = v2f(c_x + c_w, c_y); vertices[5].col = v4f(col.x, col.y, col.z, col.w); vertices[5].tCoord = v2f(t_pos_x + t_sz_x, t_pos_y); memcpy(advPointer, vertices, sizeof(vertices)); // offset means offest into the VBO ont the GPU (vbo_text) not the buffer on the CPU (advPointer) glBufferSubData(GL_ARRAY_BUFFER, 0, renderer->vbo_text.size, (void*)renderer->vbo_text.base); advPointer += 6; n_quads_written++; if(i == 0) ti.sentence_pos = v2f(c_x, c_y); if(c_y + c_h > bigges_h) bigges_h = c_y + c_h; } // TODO: Could check for the largest size instead of the size of the last characterbox. ti.sentence_size = v2f(c_x + c_w, bigges_h); glDrawArrays(GL_TRIANGLES, 0, n_quads_written * 6); return ti; } void OnGameInit(CJ_PLATFORM *platform, CJ_RENDERER *renderer, GAME_HANDLER *game) { static bool is_init = false; if(!is_init) { game->entity_batch = (ENTITY*)calloc(1000, sizeof(ENTITY)); game->entity_batch_count = 1000 * sizeof(ENTITY); game->pVertex = (CJ_VTX_QUAD*)renderer->vbo_sprite.base; V2f tp = v2f((32.0f / 512.0f), (32.0f / 512.0f)); game->player = PushEntity(game, 1); game->player->pos = v2f(1.0f, 1.7f); game->player->size = v2f(0.03f, 0.1f); game->player->col = v4f(1.0f, 1.0f, 1.0f, 1.0f); game->player->texcoord_pos = v2f(0.0f, 0.0f); game->player->texcoord_size = v2f(1.0f, 1.0f); game->monster = PushEntity(game, 1); game->monster->pos = v2f(1.0f, 1.0f); game->monster->size = v2f(0.2f, 0.2f); game->monster->col = v4f(1.0f, 1.0f, 1.0f, 0.8f); game->monster->texcoord_pos = v2f(3.0f * tp.x, 1.0f - (1.0f * tp.y)); game->monster->texcoord_size = v2f(1.0f * tp.x, 1.0f * tp.y); u32 tilemap_w = 10; u32 tilemap_h = 10; V2f start_pos = v2f(0.5f, 0.5f); V2f tile_size = v2f(0.11f, 0.11f); game->tilemap.entity = PushEntity(game, tilemap_w * tilemap_h); game->tilemap.w = tilemap_w; game->tilemap.h = tilemap_h; for(u32 y = 0; y < tilemap_h; y++) { for(u32 x = 0; x < tilemap_w; x++) { u32 index = x + (y * tilemap_w); float randW = CJ_RandFRange(0.011f, 0.074f, CJ_RAND_DONT_INCLUDE); float randH = CJ_RandFRange(0.011f, 0.074f, CJ_RAND_DONT_INCLUDE); float separation = 0.002f; printf("randW: %f\n", randW); printf("randH: %f\n", randH); V2f pos = v2f(start_pos.x + (x * tile_size.x), start_pos.y + (y * tile_size.y)); V2f size = v2f(tile_size.x - separation, tile_size.y - separation); V4f col = v4f(1.0f, 1.0f, 1.0f, 1.0f); game->tilemap.entity[index].pos = pos; game->tilemap.entity[index].size = size; game->tilemap.entity[index].col = col; game->tilemap.entity[index].texcoord_pos = v2f(0.0f * tp.x, 1.0f - (2.0f * tp.y)); game->tilemap.entity[index].texcoord_size = v2f(1.0f * tp.x, 1.0f * tp.y); game->tilemap.entity[index].ibo_id = game->tilemap.entity[0].ibo_id + (6 * index); } } game->camera = PushEntity(game, 1); game->camera->pos = v2f(0.0f, 0.0f); game->current_chosen_atlas = PushEntity(game, 1); game->current_chosen_atlas->pos = v2f(0.0f, 1.2f); game->current_chosen_atlas->size = v2f(0.6f, 0.8f); game->current_chosen_atlas->col = v4f(1.0f, 1.0f, 1.0f, 1.0f); game->current_chosen_atlas->texcoord_pos = v2f(0.0f, 0.0f); game->current_chosen_atlas->texcoord_size = v2f(1.0f, 1.0f); is_init = true; } } void OnGameUpdate(CJ_PLATFORM *platform, CJ_RENDERER *renderer, GAME_HANDLER *game) { if(GetKey(platform, GLFW_KEY_ESCAPE)) { running = false; } float mov = 0.003f; game->camera->vel = v2f(0.f, 0.f); if(GetKey(platform, GLFW_KEY_UP)) { game->camera->vel.y = mov; } if(GetKey(platform, GLFW_KEY_DOWN)) { game->camera->vel.y = -mov; } if(GetKey(platform, GLFW_KEY_RIGHT)) { game->camera->vel.x = mov; } if(GetKey(platform, GLFW_KEY_LEFT)) { game->camera->vel.x = -mov; } game->player->pos += game->camera->vel; bool clamped = false; TILEMAP *tilemap = &game->tilemap; float clamp_left = tilemap->entity[0].pos.x + tilemap->entity[0].size.x; float clamp_right = tilemap->entity[tilemap->w - 1].pos.x; float clamp_bottom = tilemap->entity[0].pos.y + tilemap->entity[0].size.y; float clamp_top = tilemap->entity[tilemap->w * tilemap->h - 1].pos.y; if(game->player->pos.x <= clamp_left) { game->player->pos.x = clamp_left; game->player->pos -= game->camera->vel; clamped = true; } if(game->player->pos.x + game->player->size.x >= clamp_right) { game->player->pos.x = clamp_right - game->player->size.x; game->player->pos -= game->camera->vel; clamped = true; } if(game->player->pos.y < clamp_bottom) { game->player->pos.y = clamp_bottom; game->player->pos -= game->camera->vel; clamped = true; } if(game->player->pos.y > clamp_top) { game->player->pos.y = clamp_top; game->player->pos -= game->camera->vel; clamped = true; } ENTITY *monster = game->monster; ENTITY *player = game->player; if(monster->pos.x != player->pos.x || monster->pos.y != player->pos.y) { V2f direction = {}; direction.x = player->pos.x - monster->pos.x; direction.y = player->pos.y - monster->pos.y; direction = Unit_v2f(direction); monster->pos.x += 0.0001f * direction.x; monster->pos.y += 0.0001f * direction.y; } if(clamped) { u32 start_entity_id = game->tilemap.entity->id; u32 n_tiles = (game->tilemap.w * game->tilemap.h); for(u32 i = start_entity_id; i < start_entity_id + n_tiles; i++) { game->entity_batch[i].pos -= game->camera->vel; } } char buffer[256] = {}; sprintf(buffer, "game.player->pos: (%f / %f)", game->player->pos.x, game->player->pos.y); static V4f tCol = COLOR_ORANGE; static V4f tCol_save = COLOR_GREEN; static V4f tCol_select = COLOR_GREEN; // can create ti before gameloop to prevent setting the color after the text is rendered. TEXT_INFO ti = {}; ti = DrawText(platform, renderer, 1, buffer, v2f(0.2f, 1.7f), tCol); if(CursorInsideText(*platform, ti)) { tCol = COLOR_RED; } else { tCol = COLOR_ORANGE; } sprintf(buffer, "time since start: (%f)", GetTime()); ti = DrawText(platform, renderer, 2, buffer, v2f(0.0f, 1.8f), COLOR_GREEN); sprintf(buffer, "platform.cursor_pos: (%f / %f)", platform->cursor_pos.x, platform->cursor_pos.y); DrawText(platform, renderer, 1, buffer, v2f(0.4f, 0.3f), v4f(1.0f, 1.0f, 1.0f, 1.0f)); ti = DrawText(platform, renderer, 2, "Select atlas", v2f(1.7f, 1.3f), tCol_select); if(CursorInsideText(*platform, ti)) { tCol_select = COLOR_RED; } else { tCol_select = COLOR_GREEN; } ti = DrawText(platform, renderer, 2, "Save map", v2f(1.7f, 1.2f), tCol_save); if(CursorInsideText(*platform, ti)) { tCol_save = COLOR_RED; if(GetMouseOnce(GLFW_MOUSE_BUTTON_LEFT)) { ti = DrawText(platform, renderer, 2, "LEFT MOUSE PRESSED", v2f(0.7f, 1.3f), tCol_select); } } else { tCol_save = COLOR_GREEN; } char dir_content[24][1024] = {}; GetDirContent(&ti, dir_content[0], "assets", 1024); u32 files_in_dir = ti.n_files_in_dir; static bool init = false; if(!init) { init = true; for(u32 i = 0; i < ti.n_files_in_dir; i++) { printf("dir_content[%d]: %s\n", i, dir_content[i]); } } static V4f cont_col[24] = {}; static V4f yes_no[2] = {}; static bool bLoad_texture = false; static char file_to_choose[256] = {}; for(u32 i = 0; i < files_in_dir; i++) { ti = DrawText(platform, renderer, 2, dir_content[i], v2f(1.6f, 1.8f - i * 0.1f), cont_col[i]); if(CursorInsideText(*platform, ti)) { cont_col[i] = COLOR_RED; if(GetMouseOnce(GLFW_MOUSE_BUTTON_LEFT) && !bLoad_texture) { strcpy(file_to_choose, dir_content[i]); bLoad_texture = true; } } else { cont_col[i] = COLOR_GREEN; } if(bLoad_texture) { ti = DrawText(platform, renderer, 2, "Load texture?", v2f(0.7f, 1.0f), COLOR_GREEN); static V4f yes_col = {}; ti = DrawText(platform, renderer, 2, "Yes", v2f(0.7f, 0.9f), yes_col); if(CursorInsideText(*platform, ti)) { yes_col = COLOR_RED; if(GetMouseOnce(GLFW_MOUSE_BUTTON_LEFT)) { LoadTexture(&renderer->texture_batch[0], file_to_choose, LOAD_IMAGE, 0); bLoad_texture = false; } } else if(GetKeyOnce(GLFW_KEY_ENTER)) { LoadTexture(&renderer->texture_batch[0], file_to_choose, LOAD_IMAGE, 0); bLoad_texture = false; } else { yes_col = COLOR_GREEN; } static V4f no_col = {}; ti = DrawText(platform, renderer, 2, "No", v2f(0.8f, 0.9f), no_col); if(CursorInsideText(*platform, ti)) { no_col = COLOR_RED; if(GetMouseOnce(GLFW_MOUSE_BUTTON_LEFT)) { bLoad_texture = false; } } else { no_col = COLOR_GREEN; } } } u32 start_entity_id = game->tilemap.entity->id; u32 n_tiles = (game->tilemap.w * game->tilemap.h); for(u32 i = 0; i < 60; i++) { SwapOrderInIBO(renderer, game->player, &game->entity_batch[i]); } for(u32 i = 0; i < n_tiles; i++) { SwapOrderInIBO(renderer, game->monster, &game->tilemap.entity[i]); } //if(game->player->pos.y < game->monster->pos.y) //{ // if(renderer->ibo_sprite.base[game->player->ibo_id] < renderer->ibo_sprite.base[game->monster->ibo_id]) // { // SwapOrderInIBO(renderer, game->player, game->monster); // } //} SwapOrderInIBO(renderer, game->player, game->monster); char iboValue_player[32] = {}; sprintf(iboValue_player, "player ibo value: %d", renderer->ibo_sprite.base[game->player->ibo_id]); char iboValue_mon[32] = {}; sprintf(iboValue_mon, "monster ibo value: %d", renderer->ibo_sprite.base[game->monster->ibo_id]); DrawText(platform, renderer, 2, iboValue_player, v2f(0.3f, 0.2f), COLOR_GREEN); DrawText(platform, renderer, 2, iboValue_mon, v2f(0.3f, 0.1f), COLOR_GREEN); } void SwapOrderInIBO(CJ_RENDERER *renderer, ENTITY *e1, ENTITY *e2) { if(e1->pos.y < e2->pos.y) { u32 e1_ibo_index = e1->ibo_id; u32 e2_ibo_index = e2->ibo_id; u32 e1_ibo_value = renderer->ibo_sprite.base[e1_ibo_index]; u32 e2_ibo_value = renderer->ibo_sprite.base[e2_ibo_index]; if(e1_ibo_value < e2_ibo_value) { for(u32 i = 0; i < 6; i++) { e1_ibo_value = renderer->ibo_sprite.base[e1_ibo_index + i]; e2_ibo_value = renderer->ibo_sprite.base[e2_ibo_index + i]; u32 temp_ibo_value = e1_ibo_value; renderer->ibo_sprite.base[e1_ibo_index + i] = e2_ibo_value; renderer->ibo_sprite.base[e2_ibo_index + i] = temp_ibo_value; } } } else if(e1->pos.y > e2->pos.y) { u32 e1_ibo_index = e1->ibo_id; u32 e2_ibo_index = e2->ibo_id; u32 e1_ibo_value = renderer->ibo_sprite.base[e1_ibo_index]; u32 e2_ibo_value = renderer->ibo_sprite.base[e2_ibo_index]; if(e1_ibo_value > e2_ibo_value) { for(u32 i = 0; i < 6; i++) { e1_ibo_value = renderer->ibo_sprite.base[e1_ibo_index + i]; e2_ibo_value = renderer->ibo_sprite.base[e2_ibo_index + i]; u32 temp_ibo_value = e1_ibo_value; renderer->ibo_sprite.base[e1_ibo_index + i] = e2_ibo_value; renderer->ibo_sprite.base[e2_ibo_index + i] = temp_ibo_value; } } } } int main() { CJ_PLATFORM platform = CreatePlatform(640, 480, "Platform", 0); CJ_RENDERER renderer = CreateRenderer(); CJ_InitRandom(); GAME_HANDLER game = {}; while(!glfwWindowShouldClose(platform.window) && running) { UpdatePlatform(&platform); glViewport(0, 0, platform.win_w, platform.win_h); RenderClear(v4f(0.1f, 0.1f, 0.1f, 1.0f)); OnGameInit(&platform, &renderer, &game); OnGameUpdate(&platform, &renderer, &game); glBindVertexArray(renderer.VAO_texture_quad); glBindBuffer(GL_ARRAY_BUFFER, renderer.vbo_sprite.id); UpdateVBO_Entity(&renderer.vbo_sprite, &game); glBufferSubData(GL_ARRAY_BUFFER, 0, renderer.vbo_sprite.size, (void*)renderer.vbo_sprite.base); u32 size_bytes = renderer.ibo_sprite.size / sizeof(u32); glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, renderer.ibo_sprite.size / sizeof(u32), (void*)renderer.ibo_sprite.base); glBindTexture(GL_TEXTURE_2D, renderer.texture_batch[0].texture_id); DrawElements(&renderer, 1, 0, game.entity_batch_count, 0); SwapBuffers(platform); } glfwTerminate(); return 0; } |