2
0

core_world_screen.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - World to screen
  4. *
  5. * Example originally created with raylib 1.3, last time updated with raylib 1.4
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. //------------------------------------------------------------------------------------
  15. // Program main entry point
  16. //------------------------------------------------------------------------------------
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [core] example - core world screen");
  24. // Define the camera to look into our 3d world
  25. Camera camera = { 0 };
  26. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  27. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  28. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  29. camera.fovy = 45.0f; // Camera field-of-view Y
  30. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  31. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  32. Vector2 cubeScreenPosition = { 0.0f, 0.0f };
  33. DisableCursor(); // Limit cursor to relative movement inside the window
  34. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  35. //--------------------------------------------------------------------------------------
  36. // Main game loop
  37. while (!WindowShouldClose()) // Detect window close button or ESC key
  38. {
  39. // Update
  40. //----------------------------------------------------------------------------------
  41. UpdateCamera(&camera, CAMERA_THIRD_PERSON);
  42. // Calculate cube screen space position (with a little offset to be in top)
  43. cubeScreenPosition = GetWorldToScreen((Vector3){cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera);
  44. //----------------------------------------------------------------------------------
  45. // Draw
  46. //----------------------------------------------------------------------------------
  47. BeginDrawing();
  48. ClearBackground(RAYWHITE);
  49. BeginMode3D(camera);
  50. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  51. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  52. DrawGrid(10, 1.0f);
  53. EndMode3D();
  54. DrawText("Enemy: 100 / 100", (int)cubeScreenPosition.x - MeasureText("Enemy: 100/100", 20)/2, (int)cubeScreenPosition.y, 20, BLACK);
  55. DrawText(TextFormat("Cube position in screen space coordinates: [%i, %i]", (int)cubeScreenPosition.x, (int)cubeScreenPosition.y), 10, 10, 20, LIME);
  56. DrawText("Text 2d should be always on top of the cube", 10, 40, 20, GRAY);
  57. EndDrawing();
  58. //----------------------------------------------------------------------------------
  59. }
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. CloseWindow(); // Close window and OpenGL context
  63. //--------------------------------------------------------------------------------------
  64. return 0;
  65. }