core_world_screen.c 3.7 KB

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