The Codes Is Broked


Joined
Jan 16, 2006
Messages
193
Age
36
Location
Room with no exit
Website
thespidermastermind.googlepages.com
Theres multiple things wrong here I'm sure, and its causing me to get a distinct disliking for C++/SDL. Is there no easier way to write out some goddamn text????? I imagine once this works, I'd have a frame for a text-writing function, but alas, the text is not shown, nor do the button captures work, which makes me think its hitting a wall somewhere.

It compiles fine, and I do have arial.ttf in the same folder as the .gpe(root of sd).

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "SDL.h"
#include "SDL_TTF.h"

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define SCREEN_DEPTH 8

using namespace std;

int main(int argc, char *argv[])
{
	SDL_Surface *screen;

	SDL_Joystick *joystick;

	TTF_Font *font = TTF_OpenFont( "arial.ttf", 12 );

	SDL_Color color = { 255,255,255,255 };
	int done=0;

	SDL_Event event;



	/* Initialize SDL */
	SDL_Init(SDL_INIT_VIDEO);

	joystick = SDL_JoystickOpen(0);

	/* Initialize the screen / window */
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);

	SDL_ShowCursor(SDL_DISABLE);

	screen=TTF_RenderText_Solid(font,"Hello World!", color);

	SDL_Flip(screen);

	SDL_PollEvent(&event);

	while (!done)
	{

		SDL_WaitEvent(&event); 

		switch (event.type) 
		{
			case SDL_JOYBUTTONDOWN:
				done = 1;
				break;
		}
	}

	SDL_Quit();

	chdir("/usr/gp2x");

	execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);

   return 0;
}

Anyone willing to help poor old higher-level-language-based me?

Edit: Why is the topic description "< br />"?
 
First off, change SCREEN_DEPTH to 16 (just a tip, it's not what's actually causing your problem).

Now, the cause of the problem is that you're overwriting the screen pointer. So, create another surface pointer, e.g:
Code:
SDL_Surface* fontsurface;

Then, you want to change the render text line to this:
Code:
fontsurface = TTF_RenderText_Solid(font,"Hello World!", color);

And finally, just before the SDL_Flip, you need this line:
Code:
SDL_BlitSurface(fontsurface, NULL, screen, NULL);

That will blit (copy) fontsurface (which contains your text) to the screen, which you can then flip to show it on the physical GP2X screen.

There are some other things you should do for good code, but that should make it work.
 
Thanks for the response, but alas, she still wont fly. However, I implemented some error checking(id rather have text output for that mind :p) and something interesting happened. Code as it is now:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "SDL.h"
#include "SDL_TTF.h"

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define SCREEN_DEPTH 16

using namespace std;

int main(int argc, char *argv[]) 
{

	SDL_Surface *screen;

	SDL_Surface *fontsurface;

	SDL_Joystick *joystick;

	TTF_Font *font = TTF_OpenFont( "arial.ttf", 12 );

	SDL_Color color = { 255,255,255,255 };
	int done=0;

	SDL_Event event;

	/* Initialize SDL */
	SDL_Init(SDL_INIT_VIDEO);

	joystick = SDL_JoystickOpen(0);
	/* Initialize the screen / window */

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);

	SDL_ShowCursor(SDL_DISABLE);

	fontsurface=TTF_RenderText_Solid(font,"Hello World!", color);

	//*****************************************************
	if(!(fontsurface=TTF_RenderText_Solid(font,"Hello World!", color)))
	{
			SDL_Quit();

			chdir("/usr/gp2x");
			execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);

			return 0;
	}
	//*****************************************************

	SDL_BlitSurface(fontsurface, NULL, screen, NULL);

	/* update the screen (aka double buffering) */
	SDL_Flip(screen);

	SDL_PollEvent(&event);

	while (!done)
	{

		SDL_WaitEvent(&event);  

		switch (event.type)
		{
			case SDL_JOYBUTTONDOWN:
				done = 1;
				break;
		}
	}

	SDL_Quit();

	chdir("/usr/gp2x");

	execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);

	return 0;
}

I added the lines with the comments around it(I get the sneaky feeling thats wrong though, and I'm going over my head :S), and it quit. Without that, nothing appears.
 
OK, something I just noticed is that you've forgotten SDL_INIT_JOYSTICK, so the buttons won't work. You need to add " | SDL_INIT_JOYSTICK" to your SDL_Init (without the quotes).

Also, you're rendering the text twice when you error-check. Change your if (!(fontsurface=blah blah)) to just if (!fontsurface).

See how that runs.
 
Back
Top