Advertisement
Guest User

Untitled

a guest
Jan 9th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. //
  2. // main.c
  3. // Extension
  4. //
  5. // Created by Dave Hayden on 7/30/14.
  6. // Copyright (c) 2014 Panic, Inc. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #include "pd_api.h"
  13.  
  14. static int update(void* userdata);
  15. const char* fontpath = "/System/Fonts/Asheville-Sans-14-Bold.pft";
  16. LCDFont* font = NULL;
  17.  
  18. #ifdef _WINDLL
  19. __declspec(dllexport)
  20. #endif
  21. int eventHandler(PlaydateAPI* pd, PDSystemEvent event, uint32_t arg)
  22. {
  23. (void)arg; // arg is currently only used for event = kEventKeyPressed
  24.  
  25. if ( event == kEventInit )
  26. {
  27. const char* err;
  28. font = pd->graphics->loadFont(fontpath, &err);
  29.  
  30. if ( font == NULL )
  31. pd->system->error("%s:%i Couldn't load font %s: %s", __FILE__, __LINE__, fontpath, err);
  32.  
  33. // Note: If you set an update callback in the kEventInit handler, the system assumes the game is pure C and doesn't run any Lua code in the game
  34. pd->system->setUpdateCallback(update, pd);
  35. }
  36.  
  37. return 0;
  38. }
  39.  
  40.  
  41. #define TEXT_WIDTH 86
  42. #define TEXT_HEIGHT 16
  43.  
  44. int x = (400-TEXT_WIDTH)/2;
  45. int y = (240-TEXT_HEIGHT)/2;
  46. int dx = 1;
  47. int dy = 2;
  48.  
  49. static int update(void* userdata)
  50. {
  51. PlaydateAPI* pd = userdata;
  52.  
  53. pd->graphics->clear(kColorWhite);
  54. pd->graphics->setFont(font);
  55. pd->graphics->drawText("Hello World!", strlen("Hello World!"), kASCIIEncoding, x, y);
  56.  
  57. PDButtons buttons;
  58. pd->system->getButtonState(&buttons, NULL, NULL);
  59. if ((buttons & kButtonB) != 0)
  60. {
  61. void* ptr = pd->system->realloc(0, 256*1024);
  62. if (ptr == NULL)
  63. {
  64. pd->system->logToConsole("malloc error");
  65. }
  66. else
  67. {
  68. pd->system->logToConsole("malloc success %p", ptr);
  69. }
  70. }
  71.  
  72. x += dx;
  73. y += dy;
  74.  
  75. if ( x < 0 || x > LCD_COLUMNS - TEXT_WIDTH )
  76. dx = -dx;
  77.  
  78. if ( y < 0 || y > LCD_ROWS - TEXT_HEIGHT )
  79. dy = -dy;
  80.  
  81. pd->system->drawFPS(0,0);
  82.  
  83. return 1;
  84. }
  85.  
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement