core_world_screen.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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(void)
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. const int screenWidth = 800;
  17. const 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 = { 0 };
  21. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f };
  22. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  23. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  24. camera.fovy = 45.0f;
  25. camera.projection = CAMERA_PERSPECTIVE;
  26. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  27. Vector2 cubeScreenPosition = { 0.0f, 0.0f };
  28. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  29. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  30. //--------------------------------------------------------------------------------------
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. UpdateCamera(&camera); // Update camera
  37. // Calculate cube screen space position (with a little offset to be in top)
  38. cubeScreenPosition = GetWorldToScreen((Vector3){cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera);
  39. //----------------------------------------------------------------------------------
  40. // Draw
  41. //----------------------------------------------------------------------------------
  42. BeginDrawing();
  43. ClearBackground(RAYWHITE);
  44. BeginMode3D(camera);
  45. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  46. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  47. DrawGrid(10, 1.0f);
  48. EndMode3D();
  49. DrawText("Enemy: 100 / 100", (int)cubeScreenPosition.x - MeasureText("Enemy: 100/100", 20)/2, (int)cubeScreenPosition.y, 20, BLACK);
  50. DrawText("Text is always on top of the cube", (screenWidth - MeasureText("Text is always on top of the cube", 20))/2, 25, 20, GRAY);
  51. EndDrawing();
  52. //----------------------------------------------------------------------------------
  53. }
  54. // De-Initialization
  55. //--------------------------------------------------------------------------------------
  56. CloseWindow(); // Close window and OpenGL context
  57. //--------------------------------------------------------------------------------------
  58. return 0;
  59. }