GP32 Need Help With Loading File Into Array


FabreNZ

Member
Joined
Dec 21, 2004
Messages
132
I have a file containing a map of a puzzle for my game:

Code:
#5###14##
2#3###7##
#7#3##182
##4#5###7
###1#3###
8###2#6##
185##6#9#
##2###8#3
##64###7#

(The # symbols will be spaces on the map, and individual numbers should be displayed separately)

The code I am trying to use is this:

Code:
#include "gp32.h"

u16 *framebuffer;
int playarea[9][9];

int main() {
    int ix;
    int iy;
    
    framebuffer = (u16*)  FRAMEBUFFER;

    gp_setCpuspeed(66);

    smc_read("dev0:\\GPMM\\puzzle1.sud",playarea,0,81);
    
    for (ix=0;ix<9;ix++)
    {
        for (iy=0;iy<9;iy++)
        {
            gp_drawString(70+(ix*20),30+(iy*20),1,playarea[ix][iy],0,framebuffer);
        }
    }
    
    while (1) {}
  
}

How am I meant to be using the smc_read function? I am sure that I am using it incorrectly. How do I get it to load the file into the 'playarea' array?

And, for those who are interested, the game is a GP32 adaptation of Su Doku. This is what it should eventually look like:

sudokutitle7bb.jpg


sudokuexample4rk.jpg
 

mrsnature

Member
Joined
Jul 22, 2003
Messages
462
for a start it would be

Code:
char playarea[9][9];

as the file is read as an array of bytes.

also, each line would be 10 bytes long as there is a newline character at the end.

there's probably a better way of doing it, wait for someone else to provide a more useful answer ;)
 

pea

developer
Joined
Oct 3, 2004
Messages
1,089
Age
44
Location
New Zealand
Website
www.projectitis.com
All true. Best way is to not have newline characters in the file at all (i.e. Strip them just before you do the final save).

Then load the whole file into memory (I can post code in a couple of days) which:
1) Read the file size
2) Reserve a buffer of that size (malloc)
3) Read the file into the buffer
4) Now copy each character from the buffer to your array

- or -

1) Simply load the file directly into your array by using the array pointer (&playarea) as the destination pointer. Make sure you only read 81 chars or you will overrun memory. And make sure you don't have any newlines in your file, and no extra characters before the start of the data.

AND - remember that the values in the array will be the ascii/ansi values for the digits 0-9, and not the numbers 0-9 themselves :)

BTW - Suduku (an NZ phenomenon) is (I think) copyright :(
 

FabreNZ

Member
Joined
Dec 21, 2004
Messages
132
Thanks for all the help!

(And I'll have to look into that possible copyright issue)

EDIT: I changed the line to "smc_read("dev0:\\GPMM\\puzzle1.sud",&playarea,0,81);" and removed the newline characters from the text file, and the GP32 resets when I run the FXE. Do you know what's wrong?

EDIT2: Commenting out the smc_read command causes a perfect 9x9 grid of "x" characters to be displayed. It is obvious that the problem lies in how I am using that command, but I am not sure where.

EDIT3: I have been continuing my program using data that isn't loaded in at runtime (while I await a solution for SMC loading), but it seems that the data I store in the array isn't the data I get back. I have "char playarea[9][9];" to initialise the array and "gp_drawString(28+(ix*22),30+(iy*22),1,playarea[ix][iy],tcol,framebuffer);" (where ix and iy are increased from 0 to 9 by a loop) to display the contents of the array. If I use "playarea[ix][iy]="1";", the data printed to the screen is a different character than the number 1. Is there a conversion between data types that I am missing?
 

mr.mirko

Certified Guru
Joined
Nov 24, 2003
Messages
471
Location
Germany
Website
mirkoroller.de
Thanks for all the help!

(And I'll have to look into that possible copyright issue)

EDIT: I changed the line to "smc_read("dev0:\\GPMM\\puzzle1.sud",&playarea,0,81);" and removed the newline characters from the text file, and the GP32 resets when I run the FXE. Do you know what's wrong?

EDIT2: Commenting out the smc_read command causes a perfect 9x9 grid of "x" characters to be displayed. It is obvious that the problem lies in how I am using that command, but I am not sure where.

EDIT3: I have been continuing my program using data that isn't loaded in at runtime (while I await a solution for SMC loading), but it seems that the data I store in the array isn't the data I get back. I have "char playarea[9][9];" to initialise the array and "gp_drawString(28+(ix*22),30+(iy*22),1,playarea[ix][iy],tcol,framebuffer);" (where ix and iy are increased from 0 to 9 by a loop) to display the contents of the array. If I use "playarea[ix][iy]="1";", the data printed to the screen is a different character than the number 1. Is there a conversion between data types that I am missing?

{ char *buffer; // pointer to free memory
int offset=0; // position in file, 0=beginning
int bytes=8192; // read bytes, in your case 9*9 = 81
int bytes_read;
buffer = (char*) malloc(bytes);
bytes_read=smc_read("dev0:\\GPMM\\your.dat",buffer,offset,bytes);
if (bytes_read == 0) gp_drawString(10,160,10,"ERROR ",0xF800,framebuffer1);
}
 
Last edited by a moderator:

mrsnature

Member
Joined
Jul 22, 2003
Messages
462
EDIT: I changed the line to "smc_read("dev0:\\GPMM\\puzzle1.sud",&playarea,0,81);" and removed the newline characters from the text file, and the GP32 resets when I run the FXE. Do you know what's wrong?

you just need "playarea" without the & as the name of an array without any []'s is a pointer to the first element.

I have "char playarea[9][9];" to initialise the array and "gp_drawString(28+(ix*22),30+(iy*22),1,playarea[ix][iy],tcol,framebuffer);" (where ix and iy are increased from 0 to 9 by a loop) to display the contents of the array. If I use "playarea[ix][iy]="1";", the data printed to the screen is a different character than the number 1. Is there a conversion between data types that I am missing?

i'm not sure if it'll make a difference but i think that should be:

Code:
playarea[ix][iy]='1';

with single quotes, using double quotes will add a string terminating character after the 1.


mr.mirko: why
Code:
 int bytes=8192;
?
 

FabreNZ

Member
Joined
Dec 21, 2004
Messages
132
Mrsnature - Mirko just had 8192 as an example.

Thanks to both of you for the help, I will see if I can get it working using that method.

EDIT: I tried this as a test:

Code:
char *buffer;
int offset=0;
int bytes=81;
int bytes_read;
buffer = (char*) malloc(bytes);
bytes_read=smc_read("dev0:\\GPMM\\PUZZLE1.SUD",buffer,offset,bytes);
if (bytes_read == 0) 
{
     gp_drawString(10,160,10,"ERROR ",0xF800,framebuffer);}
else 
{
     gp_drawString(10,10,81,bytes_read,0,framebuffer);
}

And the file it is reading is this:

#5###14##2#3###7###7#3##182##4#5###7###1#3###8###2#6##185##6#9###2###8#3##64###7#

But it outputs a line of seemingly random characters (eg no specific character is drawn in the place of a # etc)
 

FabreNZ

Member
Joined
Dec 21, 2004
Messages
132
Finally, it is working! Thank you so much!

Now that the puzzle loading function is working, I should have the first demo out within this week. Thanks to everyone that helped!
 
Top