Sign in to follow this  
Lith

SDL not drawing

Recommended Posts

Lith    429
i cant draw anything with SDL when i compile my code it gives me this warning: MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library btw im trying to draw a file that i loaded(bmp) it loads it fine

Share this post


Link to post
Share on other sites
mattd    1078
The warning is just that, a warning. It wouldn't be the cause of you not being able to render something to the screen. You are probably linking to a library that was built using a different C runtime library, and you can follow the warning's advice (add "msvcrt.lib" to the "Ignore Specific Library" setting in your project's linker settings.)

So your problem with not being able to draw is caused by something else. You'll need to give more detail. What happens, and what doesn't happen? What do you expect to happen? How do you attempt to draw the image on screen? Can you post the relevant code for us to see?

Share this post


Link to post
Share on other sites
Lith    429
Thanks for the reply

this is a bit of my main:

SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0)); //fill the screen black

//event handling
while(SDL_PollEvent(&Event)){
//If the user has Xed out the window
if( Event.type == SDL_QUIT ){
//Quit the program
quit = true;
}
}

ball.draw();
ball.handle();

update();





this is Ball::draw

void Ball::draw(){
draww(bbox.x, bbox.y, sprite, screen);
}





this is draww()

void draww( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Temporary rectangle to hold the offsets
SDL_Rect offset;

//Get the offsets
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}






this is update()

bool update(){
frame++;
if(SDL_Flip(screen) == -1){
log("could not update screen");
return false;
}
if((Fps.get_ticks() < 1000 / FRAMES_PER_SECOND)){
SDL_Delay((1000 / FRAMES_PER_SECOND) - Fps.get_ticks());
}
return true;
}


Other info:
i create an instance of the Ball class outside the main loop called ball
ball.set_sprite() sets the sprite varible for the drawing function

Share this post


Link to post
Share on other sites
Lith    429
I set bbox.y and bbox.x to 0 before the main loop

this is Ball::handle()

void Ball::handle(){
//move the ball
if (move == true){
//if the ball is suposed to be moving, move it!
switch(direction){
case 1:
bbox.x -= spd;
bbox.y -= spd;
break;

case 2:
bbox.x += spd;
bbox.y -= spd;
break;

case 3:
bbox.x += spd;
bbox.y += spd;
break;

case 4:
bbox.x -= spd;
bbox.y += spd;
break;
}
}
}


move is set to false in the constructor
spd is set to 0 before the main loop

Share this post


Link to post
Share on other sites
mattd    1078
Are you sure that the ball's sprite is being loaded correctly? I.e., is SDL_LoadBMP not returning NULL?

Could you post your code in its entirety? Use [source] ... [/source] tags to keep it formatted nicely.

Share this post


Link to post
Share on other sites
Lith    429
im sure the image is being loaded correctly because after i load it, it see if it is still NULL, and if it is i log it in the log file.

my scource code has lots of files
(not sure how to use the source tag, so ill try and use it like the code tag)


ball_class.h

#ifndef BALL_CLASS_H
#define BALL_CLASS_H

#include "SDL.h"

class Ball{
private:
SDL_Surface* sprite; //the ball sprite
bool move;

public:
SDL_Rect bbox;
short spd;
short direction;
Ball();
void set_sprite(SDL_Surface* spr, int w, int h); //sets the sprite to use
void draw(); //draws the ball on the screen
void handle(); //handles the movent and collision detecting
void start(short dir); //makes the ball start to move

};

#endif


(i know about the rules of oop, but i think its much more easyer like this)


func.h

#ifndef FUNC_H
#define FUNC_H

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "globals.h"
#include <string>
#include <fstream>

SDL_Surface *LoadImg( std::string filename ) ; //load image
void draww( int x, int y, SDL_Surface* source, SDL_Surface* destination ); //blit surface
void log( std::string message ); //log
bool update();
bool check_collision_bbox(SDL_Rect A, SDL_Rect B); //checks for collisions using bounding boxes


#endif





globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

#include "SDL.h"
#include "timer_class.h"
#include <fstream>

//engine
extern SDL_Surface* screen;
extern std::ofstream logger;
extern const int FRAMES_PER_SECOND;
extern int frame;
extern Timer Fps;

//custom

#endif




timer_class.h

#ifndef TIMER_CLASS_H
#define TIMER_CLASS_H

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include <string>
#include <fstream>

class Timer
{
private:
//The clock time when the timer started
int startTicks;

//The ticks stored when the timer was paused
int pausedTicks;

//The timer status
bool paused;
bool started;

public:
//Initializes variables
Timer();

//The various clock actions
void start();
void stop();
void pause();
void unpause();

//Gets the timer's time
int get_ticks();

//Checks the status of the timer
bool is_started();
bool is_paused();
};


#endif





ball_class.cpp

#include "SDL.h"
#include "ball_class.h"
#include "func.h"

Ball::Ball(){
sprite = NULL;
bbox.x = 0;
bbox.y = 0;
bbox.w = 0;
bbox.h = 0;
spd = 0;
move = false;
direction = 0;
}

void Ball::set_sprite(SDL_Surface* spr, int w, int h){
sprite = spr;
bbox.w = w;
bbox.h = h;
}

void Ball::draw(){
draww(bbox.x, bbox.y, sprite, screen);
}

void Ball::start(short dir){
move = true;
direction = dir;
}

void Ball::handle(){
//move the ball
if (move == true){
//if the ball is suposed to be moving, move it!
switch(direction){
case 1:
bbox.x -= spd;
bbox.y -= spd;
break;

case 2:
bbox.x += spd;
bbox.y -= spd;
break;

case 3:
bbox.x += spd;
bbox.y += spd;
break;

case 4:
bbox.x -= spd;
bbox.y += spd;
break;
}
}
}







func.cpp

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "func.h"
#include "globals.h"
#include <string>
#include <fstream>

SDL_Surface *LoadImg( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;

//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;

//Load the image using SDL_image
loadedImage = IMG_Load( filename.c_str() );

//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );

//Free the old image
SDL_FreeSurface( loadedImage );
}

if( optimizedImage != NULL )
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF ); //COULOR KEY
//~~~~~~~~~~~~~~~~~~~~~~~~~~
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}

//Return the optimized image
return optimizedImage;
}

void draww( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Temporary rectangle to hold the offsets
SDL_Rect offset;

//Get the offsets
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}

void log( std::string message )
{
//Write message to file
logger << message << std::endl;

//Flush the buffer
logger.flush();
}

bool update(){
frame++;
if( SDL_Flip( screen ) == -1 )
{
log("could not update screen");
return false;
}
if(( Fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) )
{
//Sleep the remaining frame time
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - Fps.get_ticks() );
}
return true;
}

bool check_collision_bbox( SDL_Rect A, SDL_Rect B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;

//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;

//Calculate the sides of rect B
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;

//If any of the sides from A are outside of B
if( bottomA <= topB )
{
return false;
}

if( topA >= bottomB )
{
return false;
}

if( rightA <= leftB )
{
return false;
}

if( leftA >= rightB )
{
return false;
}

//If none of the sides from A are outside B
return true;
}




globals.cpp

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "globals.h"
#include "object_class.h"
#include <string>
#include <fstream>

//engine
std::ofstream logger( "log.txt" );
SDL_Surface* screen = NULL;
const int FRAMES_PER_SECOND = 60;
int frame = 0;
Timer Fps;

//custom









timer_class.cpp

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include <string>
#include "timer_class.h"
#include <fstream>

Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}

void Timer::start()
{
//Start the timer
started = true;

//Unpause the timer
paused = false;

//Get the current clock time
startTicks = SDL_GetTicks();
}


void Timer::stop()
{
//Stop the timer
started = false;

//Unpause the timer
paused = false;
}


int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}

//If the timer isn't running
return 0;
}


void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;

//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}


void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;

//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;

//Reset the paused ticks
pausedTicks = 0;
}
}


bool Timer::is_started()
{
return started;
}



bool Timer::is_paused()
{
return paused;
}









main.cpp

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include <string>
#include <fstream>
#include "func.h"
#include "globals.h"
#include "timer_class.h"
#include "ball_class.h"

//screen vars
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//surfaces
SDL_Surface* ball_spr = NULL;

//event struct
SDL_Event Event;


int main( int argc, char* args[] )
{
log("START");

bool quit;
quit = false;

//Initialize SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ){ return 1; log("Unable to Initialize SDL");} //init SDL
else { log("Initialized SDL"); }

screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE ); //set up the screen
if( screen == NULL ){ return 1; log("Unable to initialize screen");}
else{ log("Initialized screen"); }

SDL_WM_SetCaption( "pong", NULL ); //set the window caption

//load surfaces
//ball
ball_spr = LoadImg("res/ball.bmp");
if (ball_spr = NULL){ log("error loading ball.bmp"); }

//make objects
Ball ball;
ball.bbox.x = 0; //put the ball in
ball.bbox.y = 0; //the middle of the screen
ball.spd = 0;
ball.set_sprite(ball_spr,16,16);
ball.start(1); //start the movement of the ball

//main loop
while(quit == false){
Fps.start();
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0)); //fill the screen black

//event handling
while(SDL_PollEvent(&Event)){
//If the user has Xed out the window
if( Event.type == SDL_QUIT ){
//Quit the program
quit = true;
}
}

ball.draw();
ball.handle();


update();
}

//this code will run when the program shuts down

log("END");
SDL_FreeSurface(ball_spr);
SDL_Quit();
return 0;
}

Share this post


Link to post
Share on other sites
mattd    1078
Oops.. looks like your error detection is actually causing the error :)

if (ball_spr = NULL){ log("error loading ball.bmp"); }

Should be:

if (ball_spr == NULL){ log("error loading ball.bmp"); }

Share this post


Link to post
Share on other sites

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