Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1. /*****************************************************************/
  2. /* */
  3. /* CASIO fx-9860G SDK Library */
  4. /* */
  5. /* File name : [ProjectName].c */
  6. /* */
  7. /* Copyright (c) 2006 CASIO COMPUTER CO., LTD. */
  8. /* */
  9. /*****************************************************************/
  10. #include "fxlib.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "MonochromeLib.h"
  14. #include "fonts.h"
  15.  
  16. #define SCROLL 8 //number of pixels to scroll at each press of the up/down key
  17.  
  18. unsigned long posY[2]; //array holding the scroll for each view, so that if the user comes back from a comment page, he keeps his scroll position on the post page
  19. unsigned int key;
  20. unsigned int postHeights[26];
  21. char currentView = 1; //0=post, 1=comment
  22.  
  23. const char strCmt[] = "<t This is the text post kek.> <1 Another top lvl comment > <1 Top lvl comment, with word wrap > <2 Second level comment with word wrap> <3 Third level comment with word wrap too> <4 Top-level comment that is very long like verrry long daufq> <2 Another second lvl comment>";
  24.  
  25. const char str[] = "<r /r/SandersForPresident> <p t=This is the first post.\" a=(ext.link) r/sandersforpresident u/Zezombye 45 upvotes\"> <p t=This is the title of the second post! Gotta make it long though so that I have to scroll.\" a=(self) r/askreddit u/lephenixnoirk\"> <p t=the quick brown fox jumps over the lazy dog\nTHE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!!! (why are we yelling?)\" a=goddammit test\"> <p t=Fourth post\" a=.\"";
  26.  
  27. void dispPost(char* str, int strlen);
  28. void getPost(unsigned long posY);
  29. void dispCmt(char* str, int strlen);
  30.  
  31. int AddIn_main(int isAppli, unsigned short OptionNum)
  32.  
  33. {
  34. while(1) {
  35. ML_clear_vram();
  36.  
  37. //displays the current view
  38. if (currentView == 0)
  39. dispPost(str, sizeof(str));
  40. else
  41. dispCmt(strCmt, sizeof(strCmt));
  42.  
  43. ML_pixel(127,31,1); //debug pixel, used to test the "hitbox" of posts
  44. GetKey(&key);
  45. if (key == KEY_CTRL_DOWN || key == KEY_CTRL_UP) {
  46. if (key == KEY_CTRL_DOWN)
  47. posY[currentView] += SCROLL;
  48. else if (posY[currentView] >= SCROLL)
  49. posY[currentView] -= SCROLL;
  50. }
  51. if (key == KEY_CTRL_EXE && currentView == 0) {
  52. getPost(posY[0]);
  53. }
  54. }
  55.  
  56. return 1;
  57. }
  58.  
  59. void getPost(unsigned long posY) {
  60. int height = 12;
  61. int rankOfPost = 0;
  62. while (1) {
  63. height += postHeights[rankOfPost]+3;
  64.  
  65. if (height > posY+33) {
  66. ML_clear_vram();
  67. currentView = 1;
  68. if (rankOfPost == 2) dispCmt(strCmt, sizeof(strCmt));
  69. break;
  70. }
  71. if (rankOfPost > 25)
  72. break;
  73.  
  74. rankOfPost++;
  75. }
  76. GetKey(&key);
  77. }
  78.  
  79. void dispCmt(char* str, int strlen) {
  80. int i;
  81. int currentPageHeight = 0;
  82.  
  83. for (i = 0; i < strlen; i++) {
  84. if (str[i] == '<') {
  85. if (str[i+1] == 't') { //it is the post
  86. int j = i+3;
  87. int k = 0;
  88. char post[30100];
  89.  
  90. while (str[j] != '>') { //stocks the text of the post
  91. post[k] = str[j];
  92. k++;
  93. j++;
  94. }
  95.  
  96. currentPageHeight += dispStr(post, normfont, 0, currentPageHeight-posY[1], k); //increases the page height and displays it
  97.  
  98. ML_horizontal_line(currentPageHeight-posY[1], 0, 127, 1); //draws a line after the post
  99.  
  100. currentPageHeight += 3;
  101. i = j;
  102. }
  103. if (str[i+1] >= '1' && str[i+1] <= '9') { //it is a comment
  104. int j = i+3;
  105. int k = 0;
  106. char comment[2000];
  107. int heightOfComment;
  108. while (str[j] != '>') {
  109. comment[k] = str[j];
  110. k++;
  111. j++;
  112. }
  113.  
  114. heightOfComment = dispStr(comment, normfont, 2*(str[i+1]-49), currentPageHeight-posY[1], k);
  115. for (k = 0; k < 2*(str[i+1]-49); k+=2) {
  116. ML_vertical_line(k, currentPageHeight-2-posY[1], currentPageHeight+heightOfComment+4-posY[1], 1);
  117. }
  118. //if (k > 2) ML_vertical_line(k, currentPageHeight-posY[1], currentPageHeight+heightOfComment+4-posY[1], 1);
  119. currentPageHeight += heightOfComment+7;
  120. i = j;
  121. }
  122. }
  123. }
  124. }
  125.  
  126. //interprets the html and the tags
  127. void dispPost(char* str, int strlen) {
  128. int i;
  129. int currentPostRank = 0;
  130. int currentPageHeight = 13;
  131. ML_horizontal_line(0-posY[0], 0, 127, 1);
  132. ML_horizontal_line(10-posY[0], 0, 127, 1);
  133.  
  134. for (i = 0; i < strlen; i++) {
  135. if (str[i] == '<') {
  136. if (str[i+1] == 'p') { //it is a post
  137. int j = i+2;
  138. int heightOfPost = 0;
  139. while (str[j] != '>') { //as long as the post tag doesn't end
  140. while (str[j] == ' ') //loops through the next attribute
  141. j++;
  142.  
  143. if (str[j] == 't') { //title attribute
  144. char title[330]; //because f* dynamic allocation, max title size is 300 characters
  145. int k = 0;
  146. int strlen = sizeof(title);
  147. j+=2;
  148.  
  149. while (str[j] != '"') {
  150. title[k] = str[j];
  151. k++;
  152. j++;
  153. }
  154. heightOfPost += dispStr(title, normfont, 0, currentPageHeight-posY[0], k) + 7;
  155. j++;
  156. }
  157. if (str[j] == 'a') {
  158. char attributes[64];
  159. int k = 0;
  160. int strlen = sizeof(attributes);
  161. int test;
  162. j+=2;
  163. while (str[j] != '"') {
  164. attributes[k] = str[j];
  165. k++;
  166. j++;
  167. }
  168. test = dispStr(attributes, normfont, 0, (currentPageHeight+heightOfPost)-posY[0], k);
  169. heightOfPost += test + 4;
  170. j++;
  171. }
  172. if (str[j] != ' ' && str[j] != 't' && str[j] != 'a' && str[j] != 's') {
  173. break;
  174. }
  175. }
  176.  
  177. heightOfPost += 4;
  178.  
  179. postHeights[currentPostRank] = heightOfPost;
  180. if (heightOfPost == 4)
  181. postHeights[currentPostRank] = 0; //else, non-existant posts will have a height of 4
  182.  
  183. ML_horizontal_line(currentPageHeight+postHeights[currentPostRank]-1-posY[0], 0, 127, 1);
  184.  
  185. currentPageHeight += postHeights[currentPostRank]+3;
  186. currentPostRank++;
  187. i = j;
  188. }
  189. if (str[i+1] == 'r') {
  190. int j = i+3;
  191. int k = 0;
  192. int l;
  193. int subLength = 0;
  194. char subreddit[64];
  195. while (str[j] != '>') {
  196. subreddit[k] = str[j];
  197. k++;
  198. j++;
  199. }
  200. for (l = 0; l < k; l++)
  201. subLength += normfont.length[subreddit[l]-32] +1;
  202.  
  203. dispStr(subreddit, normfont, 65-(subLength/2), 3-posY[0], k);
  204. }
  205. }
  206. }
  207. }
  208.  
  209. //displays a given string, using a given font, at the given coordinates
  210. //returns the height of the string
  211. int dispStr(char* str, struct Font font, int x2, int y, int strlen) {
  212. int k;
  213. int x = x2;
  214. int y2 = y;
  215. for (k=0; k < strlen; k++) { //browses through the given string
  216.  
  217. //word wrap: if the current character isn't a space, simply display it
  218. if (str[k] != 32 && str[k] != '\0' && str[k] != '\n') {
  219. //if (option = 1) {
  220. long j = 1 << (6*font.length[str[k]-32])-1; //initializes a long for bit checking. The long is equal to 0b10000.. with number of zeroes being the maximum length of the character, minus 1 because there's already a 1.
  221. char i;
  222.  
  223. for (i = 0; i < 6*font.length[str[k]-32]; i++) { //browses through the pixels of the character specified, shifting the 1 of j to the right each time, so that it makes 0b01000.., 0b001000... etc
  224.  
  225. if (font.ltr[str[k]-32] & (j >> i)) { //checks if the bit that is a 1 in the j is also a 1 in the character
  226.  
  227. ML_pixel(x+i%(font.length[str[k]-32]), y+i/font.length[str[k]-32], 1); //if so, locates the pixel at the coordinates, using modulo and division to calculate the coordinates relative to the top left of the character
  228. }
  229. }
  230. //}
  231.  
  232. x += font.length[str[k]-32] + 1; //now that the character has been fully displayed, shifts the cursor right by the length of character + 1
  233. } else if (str[k] == '\n') {
  234. y += 8;
  235. x = x2;
  236. } else if (str[k] == ' ') { //the current character is a space, so see if it manages to display the word without going over x=128
  237.  
  238. int i = x+4; //initializes an int to count the number of total pixels the next word takes
  239. int j = k+1; //initializes the char to the current char+1 (which is always another character)
  240. while (str[j] != 32 && str[j] != '\0') { //as long as it doesn't encounter another space or end of string
  241. i += font.length[str[j]-32]+1; //it increments i by the length of the character + 1
  242. j++;
  243. }
  244.  
  245. if (i > 128) { //the word can't be displayed, note that it is STRICTLY superior because we added an unnecessary pixel at the end
  246. y += 8; //goes on next line which is 8 pixels down
  247. x = x2; //puts cursor on beginning of line
  248. } else {
  249. x += 4;
  250. }
  251. }
  252. }
  253. return y+1-y2;
  254. }
  255. //****************************************************************************
  256. //************** ****************
  257. //************** Notice! ****************
  258. //************** ****************
  259. //************** Please do not change the following source. ****************
  260. //************** ****************
  261. //****************************************************************************
  262.  
  263.  
  264. #pragma section _BR_Size
  265. unsigned long BR_Size;
  266. #pragma section
  267.  
  268.  
  269. #pragma section _TOP
  270.  
  271. //****************************************************************************
  272. // InitializeSystem
  273. //
  274. // param : isAppli : 1 = Application / 0 = eActivity
  275. // OptionNum : Option Number (only eActivity)
  276. //
  277. // retval : 1 = No error / 0 = Error
  278. //
  279. //****************************************************************************
  280. int InitializeSystem(int isAppli, unsigned short OptionNum)
  281. {
  282. return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  283. }
  284.  
  285. #pragma section
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement