PSP-SDL-GL = Nightmare

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

Moderators: cheriff, TyRaNiD

Post Reply
nspmadero
Posts: 4
Joined: Fri Aug 28, 2009 1:44 pm

PSP-SDL-GL = Nightmare

Post by nspmadero »

I want to make a program that works the same in the PSP as in Windows, because it's faster to test something in windows than in PSP.

I try to compile in PSP the lesson09.c of the great NeHe withouth any luck.
It works flawlessly in Windows using DevCPP as compiler.

Here's the code:

Code: Select all

/*
 * This code was created by Jeff Molofee '99 
 * (ported to Linux/SDL by Ti Leggett '01)
 *
 * If you've found this code useful, please let me know.
 *
 * Visit Jeff at http://nehe.gamedev.net/
 * 
 * or for port-specific comments, questions, bugreports etc. 
 * email to [email protected]
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

/* screen width, height, and bit depth */
#define SCREEN_WIDTH  480
#define SCREEN_HEIGHT 272
#define SCREEN_BPP     24


#define NUM 50        // Number of stars

SDL_Surface *surface; // This is our SDL surface

int twinkle = 0; // Twinkling stars

typedef struct // Define the star structure
&#123;
  int r, g, b;   // Stars Color
  GLfloat dist;  // Stars Distance From Center
  GLfloat angle; // Stars Current Angle
&#125; star;

star stars&#91;NUM&#93;;       // Make an array of size 'NUM' of stars

GLfloat zoom = -15.0f; // Viewing Distance Away From Stars
GLfloat tilt = 90.0f;  // Tilt The View

GLuint loop;           // General Loop Variable
GLuint texture&#91;1&#93;;     // Storage For One Texture

void Quit&#40;int returnCode&#41; // function to release/destroy our resources and restoring the old desktop
&#123;
  SDL_Quit&#40; &#41;;         // clean up the window
  exit&#40; returnCode &#41;;  // and exit appropriately 
&#125;

void LoadGLTextures&#40; &#41; // function to load in bitmap as a GL texture
&#123;
  SDL_Surface *TexImg&#91;1&#93;; // Create storage space for the texture

  if &#40;&#40;TexImg&#91;0&#93; = SDL_LoadBMP&#40; "data/star.bmp" &#41;&#41;&#41; // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
  &#123;
    glGenTextures&#40;1, &texture&#91;0&#93;&#41;; // Create The Texture

    // Load in texture
	  glBindTexture&#40;GL_TEXTURE_2D, texture&#91;0&#93;&#41;; // Typical Texture Generation Using Data From The Bitmap

    glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR&#41;; // Linear Filtering
    glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR&#41;;
    // Generate The Texture
    glTexImage2D&#40;GL_TEXTURE_2D, 0, 3, TexImg&#91;0&#93;->w, TexImg&#91;0&#93;->h, 0, GL_RGB, GL_UNSIGNED_BYTE, TexImg&#91;0&#93;->pixels&#41;;

    SDL_FreeSurface&#40;TexImg&#91;0&#93;&#41;; // Free up any memory we may have used
  &#125;
&#125;

void handleKeyPress&#40; SDL_keysym *keysym &#41; // function to handle key press events
&#123;
  switch &#40; keysym->sym &#41;
	&#123;
	case SDLK_ESCAPE&#58;      // ESC key was pressed
    Quit&#40; 0 &#41;;
	  break;
	case SDLK_t&#58;           // 't' key was pressed
    twinkle = !twinkle;  // this toggles the twinkling of the stars
    break;
	case SDLK_UP&#58;          // Up arrow key was pressed
    tilt -= 0.5f;        // this changes the tilt of the stars 
    break;
  case SDLK_DOWN&#58;        // Down arrow key was pressed
	  tilt += 0.5f;        // this changes the tilt of the stars
    break;
	case SDLK_PAGEUP&#58;      // PageUp key was pressed
    zoom -= 0.2f;        // zoom into the scene
	  break;
	case SDLK_PAGEDOWN&#58;    // PageDown key was pressed
    zoom += 0.2f;        // zoom out of the scene
	  break;
	default&#58;
	  break;
	&#125;
&#125;

/* general OpenGL initialization function */
GLvoid initGL &#40;GLsizei Width, GLsizei Height&#41;	// We call this right after our OpenGL window is created.
&#123;
  LoadGLTextures&#40;&#41;;                      // Load in the texture
  glEnable&#40;GL_TEXTURE_2D&#41;;               // Enable Texture Mapping
  glClearColor&#40;0.0f, 0.0f, 0.0f, 0.0f&#41;;  // Set the background black
  glClearDepth&#40;1.0f&#41;;                    // Depth buffer setup
  glShadeModel&#40;GL_SMOOTH&#41;;               // Enable smooth shading
  glMatrixMode&#40;GL_PROJECTION&#41;;
  glLoadIdentity&#40;&#41;;                      // Reset The Projection Matrix
  gluPerspective&#40;45.0f, &#40;GLfloat&#41;Width/&#40;GLfloat&#41;Height, 0.1f, 100.0f&#41;;	// Calculate The Aspect Ratio Of The Window
  glMatrixMode&#40;GL_MODELVIEW&#41;;


  glBlendFunc&#40;GL_SRC_ALPHA, GL_ONE&#41;;    // Set The Blending Function For Translucency
  glEnable&#40;GL_BLEND&#41;;                   // Enable Blending

  for &#40;loop = 0;loop < NUM; loop++&#41;    // Create A Loop That Goes Through All The Stars
  &#123;
    stars&#91;loop&#93;.angle = 0.0f;             // Start All The Stars At Angle Zero
    stars&#91;loop&#93;.dist = &#40;&#40;float&#41;loop / NUM&#41; * 5.0f; // Calculate Distance From The Center
    stars&#91;loop&#93;.r = rand&#40;&#41; % 256;        // Give star&#91;loop&#93; A Random Red Intensity
    stars&#91;loop&#93;.g = rand&#40;&#41; % 256;        // Give star&#91;loop&#93; A Random Green Intensity
    stars&#91;loop&#93;.b = rand&#40;&#41; % 256;        // Give star&#91;loop&#93; A Random Blue Intensity
  &#125;
&#125;

GLvoid drawGLScene&#40; GLvoid &#41; /* Here goes our drawing code */
&#123;
  static int spin = 0;

  glClear&#40; GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT &#41;; // Clear The Screen And The Depth Buffer
  glBindTexture&#40; GL_TEXTURE_2D, texture&#91;0&#93; &#41;; // Select Our Texture
  glLoadIdentity&#40; &#41;;

  for &#40; loop = 0; loop < NUM; loop++ &#41; // Loop Through All The Stars
  &#123;
    glLoadIdentity&#40;&#41;;                                  // Reset The View Before We Draw Each Star
    glTranslatef&#40; 0.0f, 0.0f, zoom &#41;;                  // Zoom Into The Screen &#40;Using The Value In 'zoom'&#41;
    glRotatef&#40; tilt, 1.0f, 0.0f, 0.0f &#41;;               // Tilt The View &#40;Using The Value In 'tilt'&#41;
    glRotatef&#40; stars&#91;loop&#93;.angle, 0.0f, 1.0f, 0.0f &#41;;  // Rotate To The Current Stars Angle
	  glTranslatef&#40; stars&#91;loop&#93;.dist, 0.0f, 0.0f &#41;;      // Move Forward On The X Plane
    glRotatef&#40; -stars&#91;loop&#93;.angle, 0.0f, 1.0f, 0.0f &#41;; // Cancel The Current Stars Angle
    glRotatef&#40; -tilt, 1.0f, 0.0f, 0.0f &#41;;              // Cancel The Screen Tilt

    if &#40;twinkle&#41;                                       // Twinkling Stars Enabled
    &#123;
      glColor4ub&#40;stars&#91;&#40;NUM - loop&#41; - 1&#93;.r, stars&#91;&#40;NUM - loop&#41; - 1&#93;.g, stars&#91;&#40;NUM - loop&#41; - 1&#93;.b, 255&#41;; // Assign A Color Using Bytes
      glBegin&#40; GL_QUADS &#41;;                  // Begin Drawing The Textured Quad
        glTexCoord2f&#40; 0.0f, 0.0f &#41;; glVertex3f&#40; -1.0f, -1.0f, 0.0f &#41;;
        glTexCoord2f&#40; 1.0f, 0.0f &#41;; glVertex3f&#40; 1.0f, -1.0f, 0.0f &#41;;
        glTexCoord2f&#40; 1.0f, 1.0f &#41;; glVertex3f&#40; 1.0f, 1.0f, 0.0f &#41;;
        glTexCoord2f&#40; 0.0f, 1.0f &#41;; glVertex3f&#40; -1.0f, 1.0f, 0.0f &#41;;
      glEnd&#40; &#41;;
    &#125;

    glRotatef&#40; spin, 0.0f, 0.0f, 1.0f &#41;;    // Rotate The Star On The Z Axis
    glColor4ub&#40; stars&#91;loop&#93;.r, stars&#91;loop&#93;.g, stars&#91;loop&#93;.b, 255 &#41;; // Assign A Color Using Bytes
    glBegin&#40; GL_QUADS &#41;;                    // Begin Drawing The Textured Quad
	    glTexCoord2f&#40; 0.0f, 0.0f &#41;; glVertex3f&#40; -1.0f, -1.0f, 0.0f &#41;;
	    glTexCoord2f&#40; 1.0f, 0.0f &#41;; glVertex3f&#40;  1.0f, -1.0f, 0.0f &#41;;
	    glTexCoord2f&#40; 1.0f, 1.0f &#41;; glVertex3f&#40;  1.0f,  1.0f, 0.0f &#41;;
	    glTexCoord2f&#40; 0.0f, 1.0f &#41;; glVertex3f&#40; -1.0f,  1.0f, 0.0f &#41;;
	  glEnd&#40; &#41;;
    spin += 0.01f;                          // Used To Spin The Stars
    stars&#91;loop&#93;.angle += &#40;float&#41;loop / NUM; // Changes The Angle Of A Star
    stars&#91;loop&#93;.dist -= 0.01f;              // Changes The Distance Of A Star
    if &#40;stars&#91;loop&#93;.dist < 0.0f&#41;            // Is The Star In The Middle Yet
    &#123;
      stars&#91;loop&#93;.dist += 5.0f;             // Move The Star 5 Units From The Center
      stars&#91;loop&#93;.r = rand&#40;&#41; % 256;         // Give It A New Red Value
      stars&#91;loop&#93;.g = rand&#40;&#41; % 256;         // Give It A New Green Value
      stars&#91;loop&#93;.b = rand&#40;&#41; % 256;         // Give It A New Blue Value
    &#125;
  &#125;
  SDL_GL_SwapBuffers&#40;&#41;;                     // Draw it to the screen
&#125;

int main&#40; int argc, char **argv &#41;
&#123;
  int done = 0; // main loop variable
  SDL_Event event; // used to collect events

  SDL_Init&#40;SDL_INIT_VIDEO | SDL_INIT_JOYSTICK&#41;;
	// SDL_ShowCursor&#40;SDL_DISABLE&#41;;
	SDL_GL_SetAttribute&#40;SDL_GL_RED_SIZE, 8&#41;;
	SDL_GL_SetAttribute&#40;SDL_GL_GREEN_SIZE, 8&#41;;
	SDL_GL_SetAttribute&#40;SDL_GL_BLUE_SIZE, 8&#41;;
	SDL_GL_SetAttribute&#40;SDL_GL_ALPHA_SIZE, 8&#41;;
	SDL_GL_SetAttribute&#40;SDL_GL_DEPTH_SIZE, 32&#41;;
  SDL_GL_SetAttribute&#40;SDL_GL_DOUBLEBUFFER, 1&#41;; // the flags to pass to SDL_SetVideoMode

  surface = SDL_SetVideoMode&#40;                   // get a SDL surface
    SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,  
    SDL_OPENGL             // Enable OpenGL in SDL
    | SDL_GL_DOUBLEBUFFER  // Enable double buffering
    | SDL_HWPALETTE        // Store the palette in hardware
    | SDL_HWSURFACE
    | SDL_HWACCEL&#41;;

  initGL&#40;SCREEN_WIDTH, SCREEN_HEIGHT&#41;;           // initialize OpenGL
  glViewport&#40;0, 0, SCREEN_WIDTH, SCREEN_HEIGHT&#41;; // Setup our viewport.
  glMatrixMode&#40;GL_PROJECTION&#41;;                   // change to the projection matrix and set our viewing volume.
  glLoadIdentity&#40;&#41;;
  gluPerspective&#40;45.0f, SCREEN_WIDTH / SCREEN_HEIGHT, 0.1f, 100.0f &#41;; // Set our perspective
  glMatrixMode&#40;GL_MODELVIEW&#41;;                    // Make sure we're chaning the model view and not the projection
  glLoadIdentity&#40;&#41;;                              // Reset The View

  while&#40;!done&#41; // wait for events
	&#123;
    while&#40;SDL_PollEvent&#40;&event&#41;&#41; // handle the events in the queue
		&#123;
		  switch&#40;event.type&#41;
			&#123;
			case SDL_KEYDOWN&#58;
			  handleKeyPress&#40; &event.key.keysym &#41;; // handle key presses
			  break;
			case SDL_QUIT&#58;
			  done = 1; // handle quit requests
			  break;
			default&#58;
			  break;
			&#125;
		&#125;
    drawGLScene&#40;&#41;; // draw the scene
  &#125;
  Quit&#40;0&#41;; // clean ourselves up and exit
  return&#40;0&#41;; // Should never get here
&#125;
and the make is

Code: Select all

TARGET = lesson09
OBJS = lesson09.o

PSPSDK = $&#40;shell psp-config --pspsdk-path&#41;
PSPDEV = $&#40;shell psp-config -d&#41;
PSPBIN = $&#40;PSPDEV&#41;/bin
SDL_CONFIG = $&#40;PSPBIN&#41;/sdl-config

CFLAGS = -fsingle-precision-constant -O2 -g -Wall
CFLAGS += $&#40;shell $&#40;SDL_CONFIG&#41; --cflags&#41;
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBS = -lgl -lglu -lpspvfpu $&#40;shell $&#40;SDL_CONFIG&#41; --libs&#41;

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = lesson09

include $&#40;PSPSDK&#41;/lib/build.mak
and i get the next errors:

Code: Select all

C&#58;/pspsdk/psp/lib\libglu.a&#40;gluPerspectivef.o&#41;&#58; In function `gluPerspectivef'&#58;
C&#58;\msys\home\Paulo\psptoolchain\devpaks\014_pspgl\pspgl/gluPerspectivef.c&#58;30&#58; undefined reference to `glMultMatrixf'
collect2&#58; ld returned 1 exit status
make&#58; *** &#91;lesson09.elf&#93; Error 1
If i use the PSP/Nehe makefile and add the SDL libraries & stuff added by $(shell $(SDL_CONFIG) --libs) i make it to compile & link withouth any error, but in the psp i only see the cursor and then it get freezed.

Any suggestion?
KylBlz
Posts: 11
Joined: Wed Feb 11, 2009 3:52 pm

Hmm

Post by KylBlz »

I would re-install SDL or just use PSP-GL without SDL. Looks like a headder file that defined 'gluPerspectivef' is missing... also, in my makefile i have -lglut in my libs flags
willow :--)
Posts: 107
Joined: Sat Jan 13, 2007 11:50 am

Post by willow :--) »

You should have a look at the JGE++ source code which does more or less what you are trying to do (and the JGE code for windows was probably created using the NeHe tutorials, actually).

The approcah is a bit different, in that the rendering engine uses openGL for windows and linux, and GU for the PSP.
When you use the library, you call upper level method names, so that you don't have to care (too much) if you are running on windows/linux or PSP

http://code.google.com/p/wagic/source/b ... c/JGfx.cpp
http://code.google.com/p/wagic/source/b ... er_Win.cpp
http://code.google.com/p/wagic/source/b ... x/JGfx.cpp
nspmadero
Posts: 4
Joined: Fri Aug 28, 2009 1:44 pm

Post by nspmadero »

Thank you, JGE++ is ++ (object oriented), and I like my code bulky & messy :D

I was checking the OSLib thanks to other post, when i saw it in the devpack I though it was aimed to OS Development but it happend that is exactly [url=htt://http://oslib.playeradvance.org/doku.php?id=home]what i need[/url]

It works like it says on PSP, but now I want to create the libs for windows using DevCPP. (wish me luck :D)
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Re: PSP-SDL-GL = Nightmare

Post by mypspdev »

nspmadero wrote:I try to compile in PSP the lesson09.c of the great NeHe withouth any luck.
It works flawlessly in Windows using DevCPP as compiler.
All 10 NeHe lessons with GL are working and compiling well on PSP !!!
no problem at all!!

Let me know if you need the package with source code to be compiled...

ciao
liberty
Posts: 33
Joined: Wed Sep 16, 2009 11:30 am

Re: PSP-SDL-GL = Nightmare

Post by liberty »

mypspdev wrote:
nspmadero wrote:I try to compile in PSP the lesson09.c of the great NeHe withouth any luck.
It works flawlessly in Windows using DevCPP as compiler.
All 10 NeHe lessons with GL are working and compiling well on PSP !!!
no problem at all!!

Let me know if you need the package with source code to be compiled...

ciao
Would u please share the package? I have problem running the lesson 1. The system freeze and then shutdown.
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Re: PSP-SDL-GL = Nightmare

Post by mypspdev »

liberty wrote: Would u please share the package? I have problem running the lesson 1. The system freeze and then shutdown.
I'll do it, since next Saturday (17/10) I'll be out of my home for work reasons... far from my PSP development PC, I'll surely do it...
GL
SDL
SDL_gfx (RotoZoom and Primitives)
are, among others, within MyPSP dev environment (pspsdk).

..............You should have MP....
(with package and ready to be recompiled solutions ...)
Post Reply