Page 2 of 3 FirstFirst 1 2 3 LastLast
Results 11 to 20 of 30

Thread: [C/C++] Stuck On a Create-A-Level

  
  1. #11
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Oh, that makes sense. I can see how realloc() can be extremely useful and now that you mentioned the expectations of other code I can't believe I didn't think of that lol, I'm going brain dead.

    EDIT: Alright, I thought I could fix this myself (And really didn't want to ask for help again), but I've been switching everything around and can't find anything wrong with it. Can you tell me why loading my save file crashes my PSP? (It saves perfectly, but whenever I load it crashes...)

    Save function:
    Code:
    void saveGame()
    {
    	w_gameSave = fopen("file/saveData/gameSave.sav", "w");
    	fprintf(w_gameSave, "Name: %s\nHealth: %i\nRank: %i\nLevel: %i\nCredits: %i", 
    		player.getName(), player.health, player.rank, gameLevel, player.credits);
    	fclose(w_gameSave);
    	w_gameSave = NULL;
    }
    Load function:
    Code:
    void loadGame()
    {
    	
    	r_gameSave = fopen("file/saveData/gameSave.sav", "r");
    	if (!r_gameSave)
    	{
    		fclose(r_gameSave);
    		oslDebug("Error loading game save!");
    	}
    	else
    	{
    		fseek(r_gameSave, 0, SEEK_END);
    		r_saveSize = ftell(r_gameSave);
    		rewind(r_gameSave);
    		char *r_saveBuffer = (char*) malloc(r_saveSize + 1);
    		fread(r_saveBuffer, 1, r_saveSize, r_gameSave);
    		r_saveBuffer[r_saveSize] = '\0';
    		char *nameBuffer = NULL;
    		sscanf(r_saveBuffer, "Name: %s\nHealth: %i\nRank: %i\nLevel: %i\nCredits: %i", 
    			nameBuffer, &player.health, &player.rank, &gameLevel, &player.credits);
    		player.setName(nameBuffer);
    		free(r_saveBuffer);
    	}
    	fclose(r_gameSave);
    	r_gameSave = NULL;
    }
    Class:
    Code:
    class SPRITE
    {
    	public:
    	//Accessors
    		int getWalk() { return walkSpeed; }
    		int getRun() { return sprintSpeed; }
    		char *getName() { return name; }
    		void setWalk(int newWalk) { walkSpeed = newWalk; }
    		void setRun(int newRun) { sprintSpeed = newRun; }
    		void setName(char *newName) { name = newName; }
    	//Constructor and destructor.
    		SPRITE(int x, int y);
    		~SPRITE() { }
    	//Public variables.
    		int stamina, health, rank, credits;
    	private:
    	//Private variables.
    		char *name;
    		int walkSpeed, sprintSpeed;
    };
    
    SPRITE::SPRITE(int x, int y)
    {
    	walkSpeed = x;
    	sprintSpeed = y;
    	name = NULL;
    }
    I'm really sorry for asking you for so much help, but I really can't figure this out.


  2. #12
    hardhat is offline Programmer -Hacks Enthusiast
    Join Date
    May 2007
    Posts
    544
    Rep Power
    66

    Default

    Do you see the code where you say:

    Code:
    char *nameBuffer = NULL;
    Well that's fine in it's own way, but then you try to use it to read in the name from a file. That's not okay. So instead you need to save space for the result. The simplest way, would be to change that line to:

    Code:
    char nameBuffer[256];
    And then there is a place to put the result of the sscanf of the %s.

    My 2D homebrew | My 3D homebrew with Team Sushi. | My YouTube channel. | My Twitter
    Spoiler:

    hardhat: F1 == help on Windows
    J697: oh I have never used the help, so I wouldnt know
    hardhat: so that's why you're always lost. :-P

  3. #13
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Holy shit Hardhat, you never cease to amaze me o.o
    I really can't believe that that one line has been causing so much trouble...
    THANK YOU!
    *bows to the almighty hardhat*
    Once I toss some Rep around (Required to rep the same person more than once) I'll +Rep you again for all of your help.

  4. #14
    hardhat is offline Programmer -Hacks Enthusiast
    Join Date
    May 2007
    Posts
    544
    Rep Power
    66

    Default

    Does that mean that we'll see a new build soon?

    My 2D homebrew | My 3D homebrew with Team Sushi. | My YouTube channel. | My Twitter
    Spoiler:

    hardhat: F1 == help on Windows
    J697: oh I have never used the help, so I wouldnt know
    hardhat: so that's why you're always lost. :-P

  5. #15
    bloodles's Avatar
    bloodles is offline Programmer -Hacks Smarty
    Join Date
    Jun 2011
    Location
    stuck inside the matrix, darn you C++
    Posts
    169
    Rep Power
    62

    Default

    yeah, when are you going to give us a new build lol

  6. #16
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Wait, a new build of stitch'd or the Create-A-Level function? (Bare with me, I've had a few slow days lol)

  7. #17
    bloodles's Avatar
    bloodles is offline Programmer -Hacks Smarty
    Join Date
    Jun 2011
    Location
    stuck inside the matrix, darn you C++
    Posts
    169
    Rep Power
    62

    Default

    your stitch'd game of course
    Have you made a lot of changes or have you been stuck on this problem the whole time

  8. #18
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Quote Originally Posted by bloodles View Post
    your stitch'd game of course
    Have you made a lot of changes or have you been stuck on this problem the whole time
    It'll be a while until another version comes out, unfortunately. Right now I'm working on something that'll basically enable me to finish my Stitch'd game as soon as I apply it. It's a separate project that I'll release separately. I wanted to focus only on programming and less on graphics, drawing the stuff for Stitch'd and animating it takes FOREVER.

  9. #19
    hardhat is offline Programmer -Hacks Enthusiast
    Join Date
    May 2007
    Posts
    544
    Rep Power
    66

    Default

    Okay then. I just thought I'd ask.

    My 2D homebrew | My 3D homebrew with Team Sushi. | My YouTube channel. | My Twitter
    Spoiler:

    hardhat: F1 == help on Windows
    J697: oh I have never used the help, so I wouldnt know
    hardhat: so that's why you're always lost. :-P

  10. #20
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    I wish I had enough time to do both, and I would if my mom wasn't pushing me for these God damn college classes, but she's right, it's good I'm getting them free because the books alone cost like 100-200 bucks for a fucking bundle of paper... what a rip off. Anyways, hopefully I'll finish my side-project quickly and incorporate it into Stitch'd soon.

Page 2 of 3 FirstFirst 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •