You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - World to screen
  4. *
  5. * This example has been created using raylib 1.3 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free");
  19. // Define the camera to look into our 3d world
  20. Camera camera = {{ 10.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  21. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  22. Vector2 cubeScreenPosition;
  23. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  24. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  25. //--------------------------------------------------------------------------------------
  26. // Main game loop
  27. while (!WindowShouldClose()) // Detect window close button or ESC key
  28. {
  29. // Update
  30. //----------------------------------------------------------------------------------
  31. UpdateCamera(&camera); // Update camera
  32. // Calculate cube screen space position (with a little offset to be in top)
  33. cubeScreenPosition = GetWorldToScreen((Vector3){cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera);
  34. //----------------------------------------------------------------------------------
  35. // Draw
  36. //----------------------------------------------------------------------------------
  37. BeginDrawing();
  38. ClearBackground(RAYWHITE);
  39. Begin3dMode(camera);
  40. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  41. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  42. DrawGrid(10, 1.0f);
  43. End3dMode();
  44. DrawText("Enemy: 100 / 100", cubeScreenPosition.x - MeasureText("Enemy: 100 / 100", 20) / 2, cubeScreenPosition.y, 20, BLACK);
  45. DrawText("Text is always on top of the cube", (screenWidth - MeasureText("Text is always on top of the cube", 20)) / 2, 25, 20, GRAY);
  46. EndDrawing();
  47. //----------------------------------------------------------------------------------
  48. }
  49. // De-Initialization
  50. //--------------------------------------------------------------------------------------
  51. CloseWindow(); // Close window and OpenGL context
  52. //--------------------------------------------------------------------------------------
  53. return 0;
  54. }