main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "u8g2.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. /* not finished */
  5. u8g2_t u8g2;
  6. #define MAX_CHARS 30
  7. u8g2_uint_t char_offset[MAX_CHARS+1]; // one more to store the end of the string
  8. u8g2_uint_t curr_index;
  9. u8g2_uint_t curr_offset;
  10. u8g2_uint_t str_len;
  11. /* UTF8 is not supported for "s" */
  12. void init_text(const char *s)
  13. {
  14. u8g2_uint_t i;
  15. str_len = strlen(s); // calculate number of chars
  16. if ( str_len > MAX_CHARS ) // restrict the no of chars to the size of the offset table
  17. str_len = MAX_CHARS;
  18. char_offset[0] = 0; // start with 0
  19. for( i = 0; i < str_len; i++ )
  20. {
  21. char_offset[i+1] = char_offset[i];
  22. char_offset[i+1] += u8g2_GetGlyphWidth(&u8g2, s[i]); // assume s[i] is the glyph encoding --> no UTF8 support
  23. }
  24. curr_index = 0;
  25. curr_offset = 0;
  26. }
  27. void set_offset(u8g2_uint_t offset)
  28. {
  29. for(;;)
  30. {
  31. if ( char_offset[curr_index] >= offset && offset < char_offset[curr_index+1] )
  32. break;
  33. }
  34. }
  35. void draw_text(void)
  36. {
  37. }
  38. int main(void)
  39. {
  40. int x, y;
  41. int k;
  42. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  43. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  44. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  45. u8g2_SetFont(&u8g2, u8g2_font_helvB18_tn);
  46. x = 50;
  47. y = 30;
  48. for(;;)
  49. {
  50. u8g2_FirstPage(&u8g2);
  51. do
  52. {
  53. u8g2_SetFontDirection(&u8g2, 0);
  54. u8g2_DrawStr(&u8g2, x, y, "123");
  55. u8g2_SetFontDirection(&u8g2, 1);
  56. u8g2_DrawStr(&u8g2, x, y, "123");
  57. u8g2_SetFontDirection(&u8g2, 2);
  58. u8g2_DrawStr(&u8g2, x, y, "123");
  59. u8g2_SetFontDirection(&u8g2, 3);
  60. u8g2_DrawStr(&u8g2, x, y, "123");
  61. } while( u8g2_NextPage(&u8g2) );
  62. do
  63. {
  64. k = u8g_sdl_get_key();
  65. } while( k < 0 );
  66. if ( k == 273 ) y -= 7;
  67. if ( k == 274 ) y += 7;
  68. if ( k == 276 ) x -= 7;
  69. if ( k == 275 ) x += 7;
  70. if ( k == 'e' ) y -= 1;
  71. if ( k == 'x' ) y += 1;
  72. if ( k == 's' ) x -= 1;
  73. if ( k == 'd' ) x += 1;
  74. if ( k == 'q' ) break;
  75. }
  76. //u8x8_Set8x8Font(u8g2_GetU8x8(&u8g2), bdf_font);
  77. //u8x8_Draw8x8String(u8g2_GetU8x8(&u8g2), 0, 0, "Hello World!");
  78. return 0;
  79. }