Simple_message_window.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // I used the message window from Earthbound as a example.
  3. //
  4. enum messages{MESSAGE1};
  5. #define GUI_MESSAGE_WIDTH 320
  6. #define GUI_MESSAGE_HEIGHT 200
  7. #define GUI_KEY_DELAY 10
  8. #include "raylib.h"
  9. #include <string.h>
  10. // This is our message. It is passed into the message function as MESSAGE1R(see switch)
  11. // The \n characters in text that is shown in the message gui's are line feeds. They
  12. // start the next text a line down. (Set text 128 higher for larger chars.
  13. static const char message_intro[128] = "This sample illustrates a\ntext writing animation\neffect!\n\nCheck it out! ;)";
  14. // be sure to reset the framesCounter after every new message. (this is for the text effect.)
  15. static int framesCounter = 0;
  16. static bool messagedisplayed = false; // If this boolean is true than the message is completely shown.
  17. static int waitbetweenkeypresses = 0;
  18. // our global(static) functions.
  19. static void drawmessage(int x,int y,int message);
  20. static void drawroundedrect(int x, int y, int w,int h,Color col);//With this we draw our gui.
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib example.");
  28. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. waitbetweenkeypresses++;//There needs to be a time between key presses.
  36. // Here we replay the message.
  37. if(waitbetweenkeypresses>GUI_KEY_DELAY && messagedisplayed && IsKeyPressed(KEY_SPACE)){
  38. waitbetweenkeypresses = 0;//reset the between keypresses timer
  39. framesCounter = 0; // text display counter
  40. messagedisplayed = false;
  41. }
  42. //----------------------------------------------------------------------------------
  43. // Draw
  44. //----------------------------------------------------------------------------------
  45. BeginDrawing();
  46. ClearBackground(RAYWHITE);
  47. drawmessage(200,100,MESSAGE1);
  48. DrawRectangle(0,0,screenWidth,20,DARKGRAY);
  49. DrawText("Press Space for finishing message or replaying message.!",0,0,20,RAYWHITE);
  50. EndDrawing();
  51. //----------------------------------------------------------------------------------
  52. }
  53. // De-Initialization
  54. //--------------------------------------------------------------------------------------
  55. CloseWindow(); // Close window and OpenGL context
  56. //--------------------------------------------------------------------------------------
  57. return 0;
  58. }
  59. void drawmessage(int x,int y,int message){
  60. char text[128]="";
  61. switch (message){
  62. case MESSAGE1:
  63. strcat(text,message_intro);
  64. break;
  65. }
  66. framesCounter+=4;
  67. // If the user presses space than display the complete message.
  68. // (no time to wait for the typewriter effect.)
  69. if(framesCounter<strlen(text)*10){
  70. if(waitbetweenkeypresses>GUI_KEY_DELAY && IsKeyPressed(KEY_SPACE)){
  71. framesCounter=strlen(text)*10;
  72. messagedisplayed = true;
  73. waitbetweenkeypresses = 0;
  74. }
  75. }else{
  76. messagedisplayed = true;
  77. }
  78. int w=GUI_MESSAGE_WIDTH;
  79. int h=GUI_MESSAGE_HEIGHT;
  80. drawroundedrect(x,y,w,h,BLACK);
  81. drawroundedrect(x+1,y+1,w-2,h-2,LIGHTGRAY);
  82. drawroundedrect(x+6,y+6,w-12,h-12,DARKGRAY);
  83. drawroundedrect(x+8,y+8,w-16,h-16,BLACK);
  84. DrawText(TextSubtext(text, 0, framesCounter/10), x+18, y+18, 20, WHITE);
  85. //
  86. // This here figures out when the message is displayed completely and then
  87. // draws a white triangle pointing downwards. (Wait for user input here.)
  88. if(framesCounter>strlen(text)*10){
  89. x+=w-48;
  90. y+=h-16;
  91. DrawTriangle((Vector2){x,y},(Vector2){x+10,y+14},(Vector2){x+20,y},BLACK);
  92. DrawTriangle((Vector2){x+1,y+1},(Vector2){x+9,y+13},(Vector2){x+18,y+1},WHITE);
  93. }
  94. }
  95. void drawroundedrect(int x, int y, int w,int h,Color col){
  96. // our round border size.
  97. int roundsize = 20;
  98. // if messy then look here..
  99. if(w<50)roundsize/=2;
  100. if(h<50)roundsize/=2;
  101. if(w<20)roundsize/=2;
  102. if(h<20)roundsize/=2;
  103. // first draw 4 circles at the corners
  104. DrawCircle(x+roundsize,y+roundsize,roundsize,col);
  105. DrawCircle(x+w-roundsize,y+roundsize,roundsize,col);
  106. DrawCircle(x+roundsize,y+h-roundsize,roundsize,col);
  107. DrawCircle(x+w-roundsize,y+h-roundsize,roundsize,col);
  108. // Draw two rectangles on top of the circles..
  109. DrawRectangle(x,y+roundsize,w,h-roundsize*2,col);
  110. DrawRectangle(x+roundsize,y,w-roundsize*2,h,col);
  111. }