![]() |
| | #1 |
| Linux User Join Date: Nov 2005 Location: Bellingham, WA
Posts: 35
| Code: /* main.c - Game startup */
#include <stdio.h>
#include "game.h"
char *title_string = "Game framework version 1.0\n";
int quit = 0;
/* Handle all keyboard events */
void HandleKeyboardEvent(SDL_Event *event){
/* Get a convenience pointer to the structure we will use */
SDL_KeyboardEvent *key=&event->key;
/* Switch based on the key symbol */
switch(key->keysym.sym){
case SDLK_ESCAPE:
quit=1;
printf("Exiting\n");
break;
default:
break;
}
/* Switch based on the type */
/* Realeased or Pressed */
switch(key->type){
case SDL_KEYUP:
case SDL_KEYDOWN:
default:
break;
}
}
char IsMessage(void) {
char message_waiting = 0;
/* Code to check for messages goes here */
if (message_waiting) {
return 1;
} else {
return 0;
}
}
void *GetMessage(void) {
void *message;
/* code to retrieve message from queue goes here */
return message;
}
void ProcessMessage(void *message) {
/* Process the message here */
}
int main(int argc, char **argv) {
void *message;
int i;
SDL_Event event;
SDL_Surface *screen;
SDL_Surface *surface;
SDL_Color colors[256];
SDL_Color *color;
SDL_PixelFormat *fmt;
typedef struct{
SDL_Palette *palette;
Uint8 bitsperpixel;
Uint8 bytesperpixel;
Uint8 Rshift;
Uint8 Gshift;
Uint8 Bshift;
} SDL_PixelFormat;
typedef struct{
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 unused;
} SDL_Color;
color = SDL_MapRGB(fmt, 0xFF, 0xFF, 0xFF);
/* Display game title message */
fprintf(stderr, title_string);
/* Startup code goes here */
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
fprintf(stderr, "SDL Error: %s\n", SDL_GetError());
return 0;
}
atexit(SDL_Quit);
for(i=0;i<256;i++){
colors[i].r=i;
colors[i].g=i;
colors[i].b=i;
}
screen = SDL_SetVideoMode(
800, 600, /* width and height of window */
8, /* 8 bit color depth (256 colors) */
SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN /* video flags */
);
if (screen == NULL) {
fprintf(stderr, "Video error: %s\n", SDL_GetError());
exit(1);
}
SDL_WM_SetCaption(title_string, NULL);
SDL_SetColors(screen, colors, 0, 256);
/* Initialize SDL_ttf */
if(TTF_Init()==-1) {
printf("TTF_Init: %s\n", TTF_GetError());
exit(2);
}
if(!TTF_WasInit() && TTF_Init()==-1) {
printf("TTF_Init: %s\n", TTF_GetError());
exit(1);
}
/* Load a font.ttf file at size 20 */
TTF_Font *font;
font=TTF_OpenFont("electroh.ttf", 20);
if(!font) {
printf("TTF_OpenFont: %s\n", TTF_GetError());
// handle error
}
/* Display the Font */
SDL_Surface *text_surface;
if(!(text_surface=TTF_RenderText_Solid(font,"Hello World!", color))) {
//handle error here, perhaps print TTF_GetError at least
printf("Error: %s\n", TTF_GetError());
}
/* Initialize the Keyboard Event */
typedef struct{
Uint8 type;
Uint8 state;
SDL_keysym sym;
} SDL_KeyboardEvent;
typedef struct{
Uint8 scancode;
SDLKey sym;
SDLMod mod;
Uint16 unicode;
} SDL_keysym;
/* Enter main game loop */
printf("Press [ESCAPE] to quit!\n");
while(!quit){
/* Wait for an event */
if(SDL_WaitEvent(&event)){
HandleKeyboardEvent(&event);
}
}
gameloop();
/* Shutdown code goes here */
return 1;
}
Code: /* game.c - Main game loop */
#include <stdio.h>
#include "game.h"
char game_state = INIT;
void gameloop(void) {
switch(game_state) {
case INIT:
/* Game initialization code goes here */
break;
case INTRO:
/* Game intro sequence code goes here */
break;
case MENU:
/* Game menu goes here */
break;
case GAME:
/* Main game code goes here */
break;
case SHUTDOWN:
/* Game shutdown code goes here */
break;
default:
fprintf(stderr, "Bad game state");
break;
}
}
Code: /* game.h - Game loop definitions and prototypes */ #include <SDL.h> #include <SDL_ttf.h> #ifndef __GAME_H__ #define __GAME_H__ #define INIT 0 #define INTRO 1 #define MENU 2 #define GAME 3 #define SHUTDOWN 4 #endif
__________________ "Just as eating contrary to the inclination is injurious to the health, so study without desire spoils the memory, and it retains nothing that it takes in." - Leonardo De Vinci (1452-1519) |
| tuxinator is offline | |
| | #2 |
| Sr. Software Engineer Join Date: Sep 2005 Location: West Virginia
Posts: 235
| From looking at the reference for TTF_RenderText_Solid(), it seems that you want to pass the SDL_Color structure itself to the function, so, given your declaration of color as Code: SDL_Color *color; Code: text_surface=TTF_RenderText_Solid(font,"Hello World!", *color)
__________________ Insert obnoxious but pithy remark here |
| filker0 is offline | |
| | #3 |
| Linux User Join Date: Nov 2005 Location: Bellingham, WA
Posts: 35
| Well it helped get rid of the Segmentation fault error only now it doesn't even display any text at all. I'm currently in the process of trying to fix it but if anyone has any suggestions that would be great.
__________________ "Just as eating contrary to the inclination is injurious to the health, so study without desire spoils the memory, and it retains nothing that it takes in." - Leonardo De Vinci (1452-1519) |
| tuxinator is offline | |
| | #4 |
| Sweet Join Date: Aug 2002 Location: Tucson, Arizona
Posts: 1,698
| I would think you need to change you SDL_Color *color just to SDL_Color color.
__________________ Woop? |
| prog-bman is offline | |
| | #5 |
| Linux User Join Date: Nov 2005 Location: Bellingham, WA
Posts: 35
| I just recently made that change and it eliminated the segmentation fault error but now the text doesn't even show up and I think it's because the text is being displayed in black color on a black background. Does anybody know how to set the color. I've been trying to work it out all day and I can't figure it out. Here is an update of my main.c file. Code: /* main.c - Game startup */
#include <stdio.h>
#include "game.h"
char *title_string = "Game framework version 1.0\n";
int quit = 0;
/* Handle all keyboard events */
void HandleKeyboardEvent(SDL_Event *event){
/* Get a convenience pointer to the structure we will use */
SDL_KeyboardEvent *key=&event->key;
/* Switch based on the key symbol */
switch(key->keysym.sym){
case SDLK_ESCAPE:
quit=1;
printf("Exiting\n");
break;
default:
break;
}
/* Switch based on the type */
/* Realeased or Pressed */
switch(key->type){
case SDL_KEYUP:
case SDL_KEYDOWN:
default:
break;
}
}
char IsMessage(void) {
char message_waiting = 0;
/* Code to check for messages goes here */
if (message_waiting) {
return 1;
} else {
return 0;
}
}
void *GetMessage(void) {
void *message;
/* code to retrieve message from queue goes here */
return message;
}
void ProcessMessage(void *message) {
/* Process the message here */
}
int main(int argc, char **argv) {
void *message;
int i;
SDL_Event event;
SDL_Surface *screen;
SDL_Surface *surface;
SDL_Color colors[256];
SDL_Color color;
SDL_PixelFormat *fmt;
Uint32 pixel;
typedef struct{
SDL_Palette *palette;
Uint8 bitsperpixel;
Uint8 bytesperpixel;
Uint8 Rshift;
Uint8 Gshift;
Uint8 Bshift;
} SDL_PixelFormat;
typedef struct{
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 unused;
} SDL_Color;
color.r=(pixel>>screen->format->Rshift) & 0xFF;
color.g=(pixel>>screen->format->Gshift) & 0xFF;
color.b=(pixel>>screen->format->Bshift) & 0xFF;
/* Display game title message */
fprintf(stderr, title_string);
/* Startup code goes here */
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
fprintf(stderr, "SDL Error: %s\n", SDL_GetError());
return 0;
}
atexit(SDL_Quit);
for(i=0;i<256;i++){
colors[i].r=i;
colors[i].g=i;
colors[i].b=i;
}
screen = SDL_SetVideoMode(800, 600, 8, SDL_HWSURFACE);
if (screen == NULL) {
fprintf(stderr, "Video error: %s\n", SDL_GetError());
exit(1);
}
SDL_WM_SetCaption(title_string, NULL);
SDL_SetColors(screen, colors, 0, 256);
screen = SDL_CreateRGBSurface(SDL_HWSURFACE, 800, 600, 8, 256, 256, 256, NULL);
/* Initialize SDL_ttf */
if(TTF_Init()==-1) {
printf("TTF_Init: %s\n", TTF_GetError());
exit(2);
}
if(!TTF_WasInit() && TTF_Init()==-1) {
printf("TTF_Init: %s\n", TTF_GetError());
exit(1);
}
/* Load a font.ttf file at size 16 */
TTF_Font *font;
font=TTF_OpenFont("font.ttf", 16);
if(!font) {
printf("TTF_OpenFont: %s\n", TTF_GetError());
// handle error
}
/* Display the Font */
SDL_Surface *text_surface;
text_surface = TTF_RenderText_Solid(font,"Hello World!", color);
SDL_UpdateRect(text_surface, 0, 0, 0, 0);
/* Initialize the Keyboard Event */
typedef struct{
Uint8 type;
Uint8 state;
SDL_keysym sym;
} SDL_KeyboardEvent;
typedef struct{
Uint8 scancode;
SDLKey sym;
SDLMod mod;
Uint16 unicode;
} SDL_keysym;
/* Enter main game loop */
printf("Press [ESCAPE] to quit!\n");
while(!quit){
/* Wait for an event */
if(SDL_WaitEvent(&event)){
HandleKeyboardEvent(&event);
}
}
gameloop();
/* Shutdown code goes here */
TTF_CloseFont(font);
return 1;
}
__________________ "Just as eating contrary to the inclination is injurious to the health, so study without desire spoils the memory, and it retains nothing that it takes in." - Leonardo De Vinci (1452-1519) |
| tuxinator is offline | |
| | #6 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| This post may be a bit late but: You're not properly initializing color. You're setting it to a value based off a shift/and on the variable pixel - which is uninitialized. Give pixel a meaningful value before setting other variables off it. (Compile with -Wall if you're using gcc, and it should warn you about that.) Why are you typedef-ing SDL types in the middle of a function? Are these not defined in the header file? (They are for me) Furthermore, if you must typedef them, do so correctly: your definition of SDL_PixelFormat is considerably different than the one given in the documentation of SDL_PixelFormat. Using yours, you might end up with R/G/Bloss instead of R/G/Bshift. (I'm not even sure those members will contain valid data for you - you're in 8bit color. The pixel data represents an index of a color in the palette. You use that index to look up the color in the palette...) You assume all events are keyboard ones. You should check SDL_Event.type and ensure that it is a keyboard event. There are also mouse, system, joystick, etc. All of which you make into keyboard events. Finally, you're returned a pointer to an SDL_Surface by the SDL_ttf lib. Should you not free that surface? (I don't think SDL_ttf does.) You should also call TTF_Quit(). Hopefully that fixes this and other problems.
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
| | #7 |
| Linux User Join Date: Nov 2005 Location: Bellingham, WA
Posts: 35
| Ok I got the SDL_ttf to work and I found out what my errors were and what stuff could be changed. Here are my errors: 1. the color values should be 255 instead of 256. 2. I changed the screen to a 24-bit color depth. 3. The colors needed to be changed Code: color.r = 255; color.g = 255; color.b = 255;
__________________ "Just as eating contrary to the inclination is injurious to the health, so study without desire spoils the memory, and it retains nothing that it takes in." - Leonardo De Vinci (1452-1519) |
| tuxinator is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|