Sign in to follow this  

Image only sometimes shows up (SDL, Linux)

This topic is 2493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so I'm making a simple game in C on Ubuntu Linux. The problem is that the program seems to have a 50% chance of running fine. The other 50% of the time, the image shows up and flashes in random areas or doesn't show up at all. Earlier, I forgot to free some pointers and SDL_Surface's, but I fixed that, but the program still doesn't always work.

Here is the code:

main.c

[code]
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "gamelib.h"

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define BPP 32
#define FPS 30

int main(int argc, char **argv){
SDL_Init(SDL_INIT_EVERYTHING);
// Declare all variables
SDL_Surface *screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT,
BPP, SDL_SWSURFACE);
SDL_Event event;
int running = 1;

// Later get all these game objects initialized from a map file
Player *player = Player_init("main_character.png", 0, 0);
// ----------

while(running){
int timer = SDL_GetTicks();
while(SDL_PollEvent(&event)){
// Input and events
if(event.type == SDL_QUIT)
running = 0;
if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_UP:
player->yvel = -PLAYER_SPEED;
player->xvel = 0;
break;
case SDLK_DOWN:
player->yvel = PLAYER_SPEED;
player->xvel = 0;
break;
case SDLK_LEFT:
player->xvel = -PLAYER_SPEED;
player->yvel = 0;
break;
case SDLK_RIGHT:
player->xvel = PLAYER_SPEED;
player->yvel = 0;
break;
}
}
if(event.type == SDL_KEYUP){
switch(event.key.keysym.sym){
case SDLK_UP:
player->yvel = 0;
break;
case SDLK_DOWN:
player->yvel = 0;
break;
case SDLK_LEFT:
player->xvel = 0;
break;
case SDLK_RIGHT:
player->xvel = 0;
break;
}
}
}
Player_move(player);
// all painting and drawing here
SDL_FillRect(screen, NULL, 0x000000); // Black screen
Player_draw(player, screen);

// Flip here
SDL_Flip(screen);

// Delay here
if(SDL_GetTicks() - timer < 1000/FPS)
SDL_Delay((1000/FPS) - (SDL_GetTicks() - timer));
}
//clean up
Player_destroy(player);
SDL_Quit();
return 0;
}
[/code]

gamelib.h

[code]
#ifndef GAMELIB_H
#define GAMELIB_H

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"

#define WEAPON 0
#define ARMOR 1
#define USABLE 2

#define PLAYER_SPEED 4

#define PLAYER_INV_SIZE 32
#define NPC_INV_SIZE 8
#define ENEMY_INV_SIZE 4
// General Purpose functions

// Basic image loading function
SDL_Surface* loadImage(char *str);

// Basic blitting function
void applySurface(int x, int y, SDL_Surface *src, SDL_Surface *dst,
SDL_Rect *clip);

// Item functions

typedef struct ItemStruct{
char *name;
SDL_Surface *sprite;
int withPlayer;
int x, y;
int attack, defense;
int type;
} Item;

Item *Item_init(char *name, char *sprname, int wp, int x, int y, int a, int d,
int t);
void Item_destroy(Item *i);
void Item_move(Item *item, int x, int y);
void Item_draw(Item *item, SDL_Surface *screen);
// Player functions

typedef struct PlayerStruct{
int x, y, xvel, yvel;
SDL_Surface *sprite;
Item *items[PLAYER_INV_SIZE];
} Player;

Player *Player_init(char *spr, int x, int y);
void Player_destroy(Player *p);
void Player_addItem(Player *p, Item *item);
void Player_move(Player *player);
void Player_draw(Player *p, SDL_Surface *screen);

// NPC functions

typedef struct NPCStruct{
int x, y, xvel, yvel;
SDL_Surface *sprite;
Item *items[NPC_INV_SIZE];
int aggroed;
} NPC;

NPC *NPC_init(char *spr, int x, int y);
void NPC_destroy(NPC *npc);
void NPC_addItem(NPC *npc, Item *i);
void NPC_move(NPC *npc);
void NPC_draw(NPC *npc, SDL_Surface *screen);

// Enemy functions

typedef struct EnemyStruct{
int x, y, xvel, yvel;
SDL_Surface *sprite;
Item *items[ENEMY_INV_SIZE];
int type;
} Enemy;

Enemy *Enemy_init(char *spr, int x, int y);
void Enemy_destroy(Enemy *e);
void Enemy_addItem(Enemy *e, Item *i);
void Enemy_move(Enemy *e);
void Enemy_draw(Enemy *e, SDL_Surface *screen);

#endif
[/code]

gamelib.c

[code]
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "gamelib.h"

// General purpose functions

SDL_Surface* loadImage(char *str){
SDL_Surface *loaded = NULL;
SDL_Surface *opt = NULL;
loaded = IMG_Load(str);
if(loaded){
opt = SDL_DisplayFormatAlpha(loaded);
SDL_FreeSurface(loaded);
}
return opt;
}

void applySurface(int x, int y, SDL_Surface *src, SDL_Surface *dst,
SDL_Rect *clip){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(src, clip, dst, &offset);
}

// Item functions

Item *Item_init(char *name, char *sprname, int wp, int x, int y, int a, int d,
int t){
Item *i = malloc(sizeof(Item));
i->name = name;
i->sprite = loadImage(sprname);
i->withPlayer = wp;
i->x = x;
i->y = y;
i->attack = a;
i->defense = d;
i->type = t;
return i;
}

void Item_destroy(Item *i){
SDL_FreeSurface(i->sprite);
free(i);
}

void Item_move(Item *item, int x, int y){
item->x = x;
item->y = y;
}

void Item_draw(Item *item, SDL_Surface *screen){
applySurface(item->x, item->y, item->sprite, screen, NULL);
}
// Player functions

Player *Player_init(char *spr, int x, int y){
Player *p = malloc(sizeof(Player));
p->sprite = loadImage(spr);
p->x = x;
p->y = y;
p->xvel = 0;
p->yvel = 0;
return p;
}
void Player_destroy(Player *p){
SDL_FreeSurface(p->sprite);
free(p);
}
void Player_addItem(Player *p, Item *item){
int i;
for(i = 0; i < PLAYER_INV_SIZE; i++){
if(p->items[i] == NULL){
p->items[i] = item;
break;
}
}
}
void Player_move(Player *player){
player->x += player->xvel;
player->y += player->yvel;
}
void Player_draw(Player *p, SDL_Surface *screen){
applySurface(p->x, p->y, p->sprite, screen, NULL);
}
// NPC functions

NPC *NPC_init(char *spr, int x, int y){
NPC *npc = malloc(sizeof(npc));
npc->sprite = loadImage(spr);
npc->x = x;
npc->y = y;
npc->xvel = npc->yvel = 0;
return npc;
}
void NPC_destroy(NPC *npc){
SDL_FreeSurface(npc->sprite);
free(npc);
}
void NPC_addItem(NPC *npc, Item *item){
int i;
for(i = 0; i < NPC_INV_SIZE; i++){
if(npc->items[i] == NULL){
npc->items[i] = item;
break;
}
}
}
void NPC_move(NPC *npc){
npc->x += npc->xvel;
npc->y += npc->yvel;
}
void NPC_draw(NPC *npc, SDL_Surface *screen){
applySurface(npc->x, npc->y, npc->sprite, screen, NULL);
}
// Enemy functions

Enemy *Enemy_init(char *spr, int x, int y){
Enemy *e = malloc(sizeof(Enemy));
e->sprite = loadImage(spr);
e->x = x;
e->y = y;
e->xvel = e->yvel = 0;
return e;
}
void Enemy_destroy(Enemy *e){
SDL_FreeSurface(e->sprite);
free(e);
}
void Enemy_addItem(Enemy *e, Item *item){
int i;
for(i = 0; i < ENEMY_INV_SIZE; i++){
if(e->items[i] == NULL){
e->items[i] = item;
break;
}
}
}
void Enemy_move(Enemy *e){
e->x += e->xvel;
e->y += e->yvel;
}
void Enemy_draw(Enemy *e, SDL_Surface *screen){
applySurface(e->x, e->y, e->sprite, screen, NULL);
}
[/code]

Share this post


Link to post
Share on other sites
Thanks, kaktusas, I try lol.

[quote name='boogyman19946' timestamp='1298212741' post='4776671']
Does this happen as soon as you start th game or when you try to move it around with the keys? It may be a timing issue although since you're forcing your program to run in 30fps it probably isn't the case.
[/quote]

It happens as soon as I start the game. I'm installing slackware linux because I enjoy making life hard for myself so I'll test it on that and see what happens.

Share this post


Link to post
Share on other sites

This topic is 2493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this