Friday, November 8, 2013

Petit Computer Journal #24


Transferring Data from Petit Computer to PC

It's no secret that you can save the graphic screen to your SD card. What is the problem is that the data isn't stored flat. It actually is saved in character format, with block-sized groupings at that.

Discostew (of PTC MegaMan 2) wrote an article on the subject and Randomous reposted it here:

http://petitcomputer.wikia.com/wiki/GRP_File_Format_(External)

Well, that's fine and dandy if you want to use a separate program on the PC. What if you don't want to use a separate program on PC? Can you do the conversion right on Petit Computer? Of course. The question is: how difficult is it?

This is just an extremely simple problem. All I have to do is reverse the process, and I'd be done. Except, for some reason, I couldn't get it right. After four tries, and a whole day has gone, this is what I came up with:



  1. ACLS

  2. FN$="G0":'GRAPHIC FILENAME
  3. LOAD "GRP1:"+FN$,FALSE
  4. PNLTYPE "OFF"
  5. WAIT 180

  6. CX=0:CY=0
  7. FOR J=0 TO 2:FOR I=0 TO 3
  8. FOR Y=0 TO 7:FOR X=0 TO 7
  9. FOR YY=0 TO 7:FOR XX=0 TO 7

  10. GPAGE 0,2,2
  11. GPSET (I*64+X*8+XX),(J*64+Y*8+YY),CX
  12. GPAGE 1,3,3
  13. GPSET (I*64+X*8+XX),(J*64+Y*8+YY),CY
  14. CX=(CX+1)%256
  15. IF CX==0 THEN CY=CY+1

  16. NEXT:NEXT
  17. NEXT:NEXT
  18. NEXT:NEXT

  19. WAIT 60

  20. FOR J=0 TO 191:FOR I=0 TO 255
  21. GPAGE 1,2,1
  22. X=GSPOIT(I,J)
  23. GPAGE 1,3,1
  24. Y=GSPOIT(I,J)
  25. GPAGE 1,1,1
  26. C=GSPOIT(I,J)
  27. GPAGE 0,0,0
  28. GPSET X,Y,C
  29. GPAGE 1,1,1
  30. 'GPSET X,Y,0
  31. NEXT:NEXT

  32. WAIT 600


That's a two-step process. First, I load the graphic on the bottom screen, and encode the coordinates on GRP2 for x-coordinates, and GRP3 for y-coordinates. Then I use such coordinates to re-arrange the points on the top screen. You see such convoluted GPAGE commands because I'm switching back and forth among the different pages.

It works, but hardly elegant. The problem is that for some reason, my brain just can't wrap around the problem. Intellectually, I know the problem is a simple referencing problem. A pointer problem, for those of you doing in C programming language. And yet, the answer eludes me.

It's not until I'm at McDonald, getting my StreetPasses, warm and fed, that my brain functions again. Of course, after so many tries to the problem, my brain did start to warm up to the problem.

It was so trivial. I sketched the solution on the back of the receipt, and five minutes later, I was done! Here is what I came up with:


  1. LOAD "GRP1:FILENAME",FALSE
  2. PNLTYPE "OFF"
  3. GOSUB @SCAN
  4. 'SAVE GRP0 here...
  5. END

  6. @SCAN
  7. CX=0:CY=0
  8. FOR J=0 TO 2:FOR I=0 TO 3
  9. FOR Y=0 TO 7:FOR X=0 TO 7
  10. FOR YY=0 TO 7:FOR XX=0 TO 7

  11. GPAGE 1
  12. C=GSPOIT(CX,CY)
  13. GPSET CX,CY,0
  14. GPAGE 0
  15. GPSET (I*64+X*8+XX),(J*64+Y*8+YY),C
  16. CX=(CX+1)%256
  17. IF CX==0 THEN CY=CY+1

  18. NEXT:NEXT
  19. NEXT:NEXT
  20. NEXT:NEXT

  21. RETURN


Quite a big difference, wouldn't you say? I had all the pieces all along, and when I compared the solution on the receipt with my code, I realized that the code had it backwards. All I have to do then is to just switched the direction the coordinates was transfered! That's it.

Well, except it doesn't work, and Discostew pointed out that my latest code did double encoding, and the original "erroneous" code was right to begin with! Oh, well. So read in linear, and output blocks!

That's a reference, or pointer, problem. And guess what? As a programmer, you have to understand the concept of "reference". Like finger pointing to the moon. You look at the moon, instead of the finger. Simple, isn't it? Yet, so many people are confused.

So much so, that people are leaving C programming language for something easier to program. By that, they mean a programming language that does not use pointer. Guess what? The concept of reference is so universal in programming, it exists even in a pointer-less language such as BASIC.

Therefore, I say, you must learn the concept of pointers. If you don't, you just limit yourself to the problems you can solve.

Oh, and about this? I don't know why I was having such troubles. But I did solve it, and that's the important thing. The next time such a problem come up again, I'll be ready.

No comments:

Post a Comment