forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

grr.... General C - 1D array to 2D array nothing is working!

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
sg57



Joined: 14 Oct 2005
Posts: 154

PostPosted: Tue Nov 07, 2006 4:09 pm    Post subject: grr.... General C - 1D array to 2D array nothing is working! Reply with quote

Ok, Ive asked around alot, tried alot of variations, etc yet I still cant get it working like it should.

All the results i get are

***
* **
** *
***

When it should be

***
***
***
***

I have this one array that gets a files data loaded into it.

map[592];

An example ofa map is:
Code:

#####    ##    ##    #      #
##### ##### ##### ######## ##
#####    ## #  ##    #### ###
######## ## ## ##### ### ####
#####    ##    ##    ## #####
#############################
######                 ######
######                 ######
######                 ######
######                 ######
######                 ######
######                 ######
######   #       #     ######
######F  #   # * # * S ######
#############################
###########   ###############
############# ###############
############# ###############
###########     #############
abc


the last 3 chars are the password for the level.

To make it easier to work with, im trying to get it into a 2D array

level[19][30];

19 because i dont WANT the password in mine, 30 because thats how many chars wide i want to read in. so, i basically want to transfer each line of that, into the 2D array according to each line. So, my current method is:
Code:

     j=0;
     for(i=0; i<19; i++) {
                       for (z=0; z<30; z++) {
                            if(map[j] == '\n') {}
                            else {
                                  level[i][z]=map[j];
                            }
                             j++;
                       }
     }

This produces the stairs effect
***
* **
** *
***
Ive tried just about everything to no prevail.

If you need know more info to help solve this problem, please ask and i will supply. This little problem has held me back from progressing for 3 days now, its really getting annoying :(
Back to top
View user's profile Send private message
ryoko_no_usagi



Joined: 29 Nov 2005
Posts: 65

PostPosted: Tue Nov 07, 2006 5:51 pm    Post subject: Reply with quote

Make sure the map is not using CRLF. The size of the array suggests this (592 = 19 * 31 + 3).
Back to top
View user's profile Send private message Send e-mail MSN Messenger
sg57



Joined: 14 Oct 2005
Posts: 154

PostPosted: Tue Nov 07, 2006 7:54 pm    Post subject: Reply with quote

Yes it is using Character Return Line Feed. Im guessing that is just the new line char right? Cause ive done an if statement saying whether map[j] == '\n' then just dont transfer it over

It, is, one character right? CRLF?
Back to top
View user's profile Send private message
adrahil



Joined: 16 Mar 2006
Posts: 277

PostPosted: Tue Nov 07, 2006 8:06 pm    Post subject: Reply with quote

Code:
     j=0;
     for(i=0; i<19; i++) {
                       for (z=0; z<30; z++) {
                            if(map[j] == '\n') {}
                            else {
                                  level[i][z]=map[j];
                            }
                             j++;
                       }
     }

Well, you still increment z and i even if the map[j] is \n :) That's probably the problem, because it makes it skip some stuff... Like, if you have this:
Code:
**
**
, and loop for let's say two, you get this in the array:
Code:
{{"*", "*"}, // end of first two, increment i...
{0, "*"}, // continue counting, but the \n also increments the loop's i/j or whatever... so it makes a shift...
...
}
Back to top
View user's profile Send private message
ryoko_no_usagi



Joined: 29 Nov 2005
Posts: 65

PostPosted: Tue Nov 07, 2006 8:08 pm    Post subject: Reply with quote

I meant that the map might terminate each line with "\r\n" (common on windows) as opposed to just a '\n" (UNIX method). So that's two bytes instead of just one.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
adrahil



Joined: 16 Mar 2006
Posts: 277

PostPosted: Tue Nov 07, 2006 8:08 pm    Post subject: Reply with quote

Oh, and learn serialization, it will help you a lot in a multi-level kind of game ;)
Back to top
View user's profile Send private message
sg57



Joined: 14 Oct 2005
Posts: 154

PostPosted: Wed Nov 08, 2006 10:21 am    Post subject: Reply with quote

AHH!

That must be it... I knew each line has a \n at the end, but i didnt know Windows has it \r\n or some variation. Ill try it out :)

EDIT

Ah, your right (3 posts above me). i/z is still incrementing... Therefore, my new version:
Code:

     j=0;
     for(i=0; i<19; i++) {
                       for (z=0; z<29; z++) {
                            level[i][z]=map[j];
                            j++;
                       }
                       level[i][29]='\0';
     }


EDIT

Spotted the problem again. Sure, the 2d array is fine, but the map 'j' indicator willstill point ot the \n. so, ive added, right below level[i][29]='\0', j+=2 (ill experiment tosee if it works)
Back to top
View user's profile Send private message
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Wed Nov 08, 2006 1:54 pm    Post subject: Reply with quote

There are easy and hard ways to parse a ascii map. I think you digged the hard way :)
I'd suggest reading in line after line from the file (fgets), then copying 30 bytes (str/memcpy) from that line into your level[i] array.
_________________
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Back to top
View user's profile Send private message Visit poster's website
sg57



Joined: 14 Oct 2005
Posts: 154

PostPosted: Wed Nov 08, 2006 4:37 pm    Post subject: Reply with quote

Ya - ive been told to use memcpy. i have no experience with it. as for fgets - i dont have access to that rather sceIo* calls, so i should be fine. just need to practice with memcpy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group