Create_Rotate_ship_center.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "raylib.h"
  2. int main(void)
  3. {
  4. // Initialization
  5. //--------------------------------------------------------------------------------------
  6. const int screenWidth = 800;
  7. const int screenHeight = 450;
  8. InitWindow(screenWidth, screenHeight, "raylib example.");
  9. // Create a Image in memory
  10. RenderTexture2D player = LoadRenderTexture(16, 16);
  11. // Draw our ship..
  12. BeginTextureMode(player);
  13. ClearBackground(BLANK);
  14. for(int y=player.texture.height;y>0;y--){
  15. BeginTextureMode(player);
  16. DrawLine(0+y/2,player.texture.height-y,player.texture.width-y/2,player.texture.height-y,WHITE);
  17. EndTextureMode();
  18. }
  19. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  20. //--------------------------------------------------------------------------------------
  21. // Several variables we use for ship rotation speed and our current angle.
  22. int shipTurnSpeed=3;
  23. int shipAngle=0;
  24. // Main game loop
  25. while (!WindowShouldClose()) // Detect window close button or ESC key
  26. {
  27. // Update
  28. //----------------------------------------------------------------------------------
  29. for(int i=0;i<shipTurnSpeed;i++){
  30. if(IsKeyDown(KEY_LEFT)){
  31. shipAngle--;
  32. }
  33. if(IsKeyDown(KEY_RIGHT)){
  34. shipAngle++;
  35. }
  36. if(shipAngle<0)shipAngle=359;
  37. if(shipAngle>359)shipAngle=0;
  38. }
  39. //----------------------------------------------------------------------------------
  40. // Draw
  41. //----------------------------------------------------------------------------------
  42. BeginDrawing();
  43. ClearBackground(BLACK);
  44. DrawText("Use Cursor Left and Right to Rotate Ship - escape to exit.",2,2,20,DARKGRAY);
  45. // Draw a texture from rectangle to target rectangle. Note we use the - for opengl reasons!
  46. DrawTexturePro(player.texture, (Rectangle){0,0,player.texture.width,-player.texture.height},
  47. (Rectangle){320,200,player.texture.width,player.texture.height},
  48. (Vector2){player.texture.width/2,player.texture.height/2},shipAngle,WHITE);
  49. //(Rectangle){0,0,player.texture.width,-player.texture.height} == << what part of texture to draw
  50. // (Rectangle){320,200,player.texture.width,player.texture.height} == << where to draw it to on the screen
  51. //(Vector2){player.texture.width/2,player.texture.height/2} == << rotation center
  52. //shipAngle == << rotation in degrees
  53. //
  54. EndDrawing();
  55. //----------------------------------------------------------------------------------
  56. }
  57. // De-Initialization
  58. UnloadRenderTexture(player); // Unload render texture
  59. //--------------------------------------------------------------------------------------
  60. CloseWindow(); // Close window and OpenGL context
  61. //--------------------------------------------------------------------------------------
  62. return 0;
  63. }