In need of help with C

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
kozine
Posts: 6
Joined: Sat Mar 18, 2006 7:20 pm
Location: England, United Kingdom
Contact:

In need of help with C

Post by kozine »

Hey guys,
This is my first post at these forums. Yesterday I moved from LUA to C and am in need of help.

My code compiles successfully, when I load it up on my PSP I just get a blank screen. My PSP doesnt crash as I can still return to the eloader. Here is my code:

Code: Select all

#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include "graphics.h"

#define MAX&#40;X, Y&#41; &#40;&#40;X&#41; > &#40;Y&#41; ? &#40;X&#41; &#58; &#40;Y&#41;&#41;
#define printf pspDebugScreenPrintf

PSP_MODULE_INFO&#40;"Button Basher", 0, 1, 1&#41;;


/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCall  back&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;  

          return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid  , 0, 0&#41;;
          &#125;

          return thid;
&#125;

int main&#40;&#41; &#123;
	char *buffer = "menubg.png";
	Image* menubg;

	pspDebugScreenInit&#40;&#41;;
	SetupCallbacks&#40;&#41;;
	initGraphics&#40;&#41;;

	sprintf&#40;buffer, "menubg.png"&#41;;
	menubg = loadImage&#40;buffer&#41;;

	if &#40;!menubg&#41; &#123;
			//Image load failed
			printf&#40;"Image load failed! Please email [email protected]!\n"&#41;;
	&#125; else &#123;
		int x = 0;
		int y = 0;
		sceDisplayWaitVblankStart  &#40;&#41;;
		while &#40;x < 480&#41; &#123;
			while &#40;y < 272&#41; &#123;
				blitAlphaImageToScreen&#40;0,   0, 480, 272, menubg, x, y&#41;;
			
                    &#125;
			flipScreen&#40;&#41;;
		&#125;
	&#125;
	sceKernelSleepThread&#40;&#41;;
	return 0;
&#125;
Ive placed the image in the same folder as my eboot.pbp file aswell on my PSP. Any help is appreciated!
Image
Lynxie
Posts: 2
Joined: Sat Apr 15, 2006 6:42 am

Post by Lynxie »

Increment x and y and things will move, you'll find others by yourself :)
Last edited by Lynxie on Sat Apr 15, 2006 6:47 am, edited 1 time in total.
kozine
Posts: 6
Joined: Sat Mar 18, 2006 7:20 pm
Location: England, United Kingdom
Contact:

Post by kozine »

What do you mean? Dont forget im new to this lol ;)
Image
Lynxie
Posts: 2
Joined: Sat Apr 15, 2006 6:42 am

Post by Lynxie »

Code: Select all

int x = 0;
      int y = 0;
      sceDisplayWaitVblankStart  &#40;&#41;;
      while &#40;x < 480&#41; &#123;
         while &#40;y < 272&#41; &#123;
            blitAlphaImageToScreen&#40;0,   0, 480, 272, menubg, x, y&#41;;
         
                    &#125;
         flipScreen&#40;&#41;;
      &#125; 
x and y are always equals to 0.
User avatar
neofar
Posts: 47
Joined: Wed Jan 21, 2004 2:40 am
Location: Spain
Contact:

Post by neofar »

Code: Select all

while &#40;y < 272&#41; &#123; 
            blitAlphaImageToScreen&#40;0,   0, 480, 272, menubg, x, y&#41;; 
                   &#125; 
the program NEVER exit of this lines ...... y is always < 272 .... never exit!!!!!!!
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

char *buffer = "menubg.png";
Image* menubg;

sprintf(buffer, "menubg.png");
menubg = loadImage(buffer);

this is a pointer you can't assign to a pointer some data, plus you try to sprintf into the space pointed by buffer which wasn't even alloched so you must change this in this way:

Code: Select all

char *buffer;
#define IMAGE "menubg.png"
   buffer = malloc&#40;strlen&#40;IMAGE&#41;&#41;;
   sprintf&#40;buffer, "menubg.png"&#41;;
i'm not so sure if image *menubg is right because i don't know how the libpng work
PeterM
Posts: 125
Joined: Sat Dec 31, 2005 7:25 pm
Location: Edinburgh, UK
Contact:

Post by PeterM »

I know it's not what you want to hear, but you really really really should learn C on a PC compiler with a real time debugger before trying on the PSP.

You will save yourself many frustrating late nights. It cannot be understated.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Code: Select all

char *buffer; 
#define IMAGE "menubg.png" 
   buffer = malloc&#40;strlen&#40;IMAGE&#41;&#41;; 
   sprintf&#40;buffer, "menubg.png"&#41;; 
This is wrong. It should be

Code: Select all

  buffer = malloc&#40;strlen&#40;IMAGE&#41;+1&#41;;
to avoid a stack overrun.

The original code did have storage allocated for buffer because it was initialised, unfortunately, you are not supposed to write to constant strings in C.
ie.

Code: Select all

char *buffer = "hello"; //OK, buffer points to constant string
buffer&#91;0&#93; = 'b'; //not OK, writing to constant string
or

Code: Select all

char buffer&#91;&#93;="hello";//OK, buffer points to variable string
buffer&#91;0&#93; = 'b'; //fine
Jim
Sonicadvance1
Posts: 10
Joined: Thu Oct 13, 2005 8:35 am

Post by Sonicadvance1 »

if you look closely at his code you can see this

Code: Select all

      while &#40;x < 480&#41; &#123;
         while &#40;y < 272&#41; &#123;
            blitAlphaImageToScreen&#40;0,   0, 480, 272, menubg, x, y&#41;;
         
                    &#125;
         flipScreen&#40;&#41;;
      &#125; 
he has two while loops and he blits the image to the screen which is 480x272 for an infinite amount of times and if the inside loop accually completed, it would flip the screen each time the outside loop went through.
Not too sure as to why he would want to blit the same image 272*480 times though. Prolly just a noob mistake.

And also, welcome to C/C++. Please try to enjoy yourself
Post Reply