core_world_screen.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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-2022 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 - 3d camera free");
  24. // Define the camera to look into our 3d world
  25. Camera camera = { 0 };
  26. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f };
  27. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  28. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  29. camera.fovy = 45.0f;
  30. camera.projection = CAMERA_PERSPECTIVE;
  31. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  32. Vector2 cubeScreenPosition = { 0.0f, 0.0f };
  33. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  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);
  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("Text is always on top of the cube", (screenWidth - MeasureText("Text is always on top of the cube", 20))/2, 25, 20, GRAY);
  56. EndDrawing();
  57. //----------------------------------------------------------------------------------
  58. }
  59. // De-Initialization
  60. //--------------------------------------------------------------------------------------
  61. CloseWindow(); // Close window and OpenGL context
  62. //--------------------------------------------------------------------------------------
  63. return 0;
  64. }