Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMotoky

Posted 15 March 2013 - 06:52 AM

Ok, I'm working on another tutorial and now the screen is appearing then disappearing pretty much instantly. None of the images are being displayed.

 

It's the program which gets you to play sounds and such by including the SDL_mixer library which I have included properly just like with the SDL_ttf library.

 

 

I've tried running the program without any code from the SDL_mixer and even the SDL_ttf library and it's still doing the same thing, so it can't be any of them.

 

I made a seperate class for the SDL_mixer library to make it easier for me to read.

 

lazyfoo.net/SDL_tutorials/lesson11/index.php

 

If you go to that link, scroll down and download the tutorial. That's what my code pretty much looks like, but without the SDL_image library. I've even copied and pasted that exact same code, with a few alterations for it to not give me any errors and it does the same thing. The program just turns off straight away.

 

I've even tried creating a new project again, but still not working...


#2Motoky

Posted 15 March 2013 - 06:48 AM

Ok, I'm working on another tutorial and now the screen is appearing then disappearing pretty much instantly. None of the images are being displayed.

 

It's the program which gets you to play sounds and such by including the SDL_mixer library which I have included properly just like with the SDL_ttf library.

 

 

I've tried running the program without any code from the SDL_mixer and even the SDL_ttf library and it's still doing the same thing, so it can't be any of them.

 

I made a seperate class for the SDL_mixer library to make it easier for me to read.

 

 


// Playing Sounds by Jamie Rudge

// Demonstrates the use of sounds in an SDL Application

// Main.cpp

#include <SDL.h>

#include <SDL_ttf.h>

#include <SDL_events.h>

#include "Sound.h"

#include <string>

using namespace std;

// Video Mode Dimensions and Bit Depth

const int SCRN_WIDTH = 640;

const int SCRN_HEIGHT = 480;

const int SCRN_BPP = 32;

 

SDL_Surface* videoSurface = NULL; // Video Surface

SDL_Surface* background = NULL; // Background Bitmap

SDL_Surface* instruction1 = NULL; // Instruction Font 1

SDL_Surface* instruction2 = NULL; // Instruction Font 2

SDL_Surface* instruction3 = NULL; // Instruction Font 3

SDL_Event event; // Event Handling

 

TTF_Font* font = NULL;

SDL_Color fontColor = {255, 255, 255};

 

Sound* soundMgr = NULL;

//===================================================================//

// FUNCTION DECLARATION //

//===================================================================//

bool initSDL(); // Initiliaze the SDL

// Application

 

bool initTTF(); // Initialize True Type Fonts

 

bool loadFiles(); // Load the files

 

SDL_Surface* loadBMP(string filename); // Load the bitmaps and store

// them in an SDL Surface.

 

void applySurface(int x, int y, // Apply the surface to the

SDL_Surface* src, // video surface.

SDL_Surface* dest,

SDL_Rect* clip = NULL);

 

void setText(); // Set up the text for each

// command.

 

void renderText(); // Renders the text to the

// screen.

 

void shutDown(); // Free the surfaces memory and

// shutdown the application.

//===================================================================//

int main(int argc, char** argv)

{

bool quit = false;

 

 

if(!initSDL())

return -1;

 

 

if(!loadFiles())

return -1;

 

 

if(!soundMgr->initSound())

 

 

return -1;

setText();

applySurface(0, 0, background, videoSurface, 0);

renderText();

 

 

while(!quit)

{

 

 

if(SDL_PollEvent(&event))

{

 

 

if(event.type == SDL_KEYDOWN)

{

 

 

if(event.key.keysym.sym == SDLK_1)

{

soundMgr->playSound(1);

}

 

 

 

if(event.key.keysym.sym == SDLK_2)

{

soundMgr->playSound(2);

}

 

 

if(event.key.keysym.sym == SDLK_3)

{

soundMgr->playSound(3);

}

 

 

if(event.key.keysym.sym == SDLK_4)

{

soundMgr->playSound(4);

}

 

 

if(event.key.keysym.sym == SDLK_9)

{

soundMgr->pauseSound();

}

 

 

if(event.key.keysym.sym == SDLK_0)

{

soundMgr->stopSound();

}

}

 

 

else if(event.type == SDL_QUIT)

{

quit =

 

true;

}

}

 

 

if(SDL_Flip(videoSurface) == -1)

{

 

 

return -1;

}

}

shutDown();

 

 

return 0;

}

bool

 

 

initSDL()

{

 

 

if(SDL_Init(SDL_INIT_EVERYTHING) == -1)

 

 

return false;

videoSurface = SDL_SetVideoMode(SCRN_WIDTH, SCRN_HEIGHT, SCRN_BPP,

SDL_SWSURFACE);

 

 

if(videoSurface == NULL)

 

 

return false;

 

 

if(!initTTF())

 

 

return false;

SDL_WM_SetCaption(

 

"Playing Sound", NULL);

 

 

return true;

}

bool

 

 

loadFiles()

{

background = loadBMP(

 

"background.bmp");

 

 

if(background == NULL)

 

 

return false;

 

 

if(!soundMgr->loadFile())

 

 

return false;

font = TTF_OpenFont(

 

"robotic.ttf", 24);

 

 

if(font == NULL)

 

 

return false;

 

 

return true;

}

bool

 

 

initTTF()

{

 

 

if(TTF_Init() == -1)

 

 

return false;

 

 

return true;

}

SDL_Surface* loadBMP(string filename)

{

SDL_Surface* tempSurface = NULL;

SDL_Surface* optimizedImage = NULL;

tempSurface = SDL_LoadBMP(filename.c_str());

 

 

if(tempSurface != NULL)

{

optimizedImage = SDL_DisplayFormat(tempSurface);

SDL_FreeSurface(tempSurface);

 

 

if(optimizedImage != NULL)

{

Uint32 color = SDL_MapRGB(optimizedImage->format,

0, 0xFF, 0xFF);

SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, color);

}

}

 

 

return optimizedImage;

}

void

 

 

applySurface(int x, int y, SDL_Surface* src, SDL_Surface* dest,

SDL_Rect* clip)

{

SDL_Rect offset;

offset.x = x;

offset.y = y;

SDL_BlitSurface(src, clip, dest, &offset);

}

void

 

 

setText()

{

instruction1 = TTF_RenderText_Solid(font,

 

 

"Press 1, 2, 3, or 4 to play a sound effect", fontColor);

instruction2 = TTF_RenderText_Solid(font,

 

 

"Press 9 to play or pause the music", fontColor);

instruction3 = TTF_RenderText_Solid(font,

 

 

"Press 0 to stop the music", fontColor);

}

void

 

 

renderText()

{

 

 

// Apply the first set of instructions

applySurface((SCRN_WIDTH/2) - (instruction1->w/2),

(SCRN_HEIGHT/2)/2,

instruction1, videoSurface);

 

 

// Apply the second set of instructions

applySurface((SCRN_WIDTH/2) - (instruction2->w/2),

(SCRN_HEIGHT/2),

instruction2, videoSurface);

 

 

// Apply the last set of instructions

applySurface((SCRN_WIDTH/2) - (instruction2->w/2),

(SCRN_HEIGHT/2)/2 + (SCRN_HEIGHT/2),

instruction3, videoSurface);

}

void

 

 

shutDown()

{

SDL_FreeSurface(background);

SDL_FreeSurface(instruction1);

SDL_FreeSurface(instruction2);

SDL_FreeSurface(instruction3);

soundMgr->shutDown();

TTF_CloseFont(font);

TTF_Quit();

SDL_Quit();

}


#1Motoky

Posted 15 March 2013 - 06:44 AM

Ok, I'm working on another tutorial and now the screen is appearing then disappearing pretty much instantly. None of the images are being displayed.

 

It's the program which gets you to play sounds and such by including the SDL_mixer library which I have included properly just like with the SDL_ttf library.

 

 

I've tried running the program without any code from the SDL_mixer and even the SDL_ttf library and it's still doing the same thing, so it can't be any of them.

 

I made a seperate class for the SDL_mixer library to make it easier for me to read.

 

 

 

// Playing Sounds by Jamie Rudge

// Demonstrates the use of sounds in an SDL Application

// Main.cpp

#include

 

 

<SDL.h>

#include

 

 

<SDL_ttf.h>

#include

 

 

<SDL_events.h>

#include

 

 

"Sound.h"

#include

 

 

<string>

using

 

 

namespace std;

// Video Mode Dimensions and Bit Depth

const

 

 

int SCRN_WIDTH = 640;

const

 

 

int SCRN_HEIGHT = 480;

const

 

 

int SCRN_BPP = 32;

SDL_Surface* videoSurface = NULL;

 

// Video Surface

SDL_Surface* background = NULL;

 

// Background Bitmap

SDL_Surface* instruction1 = NULL;

 

// Instruction Font 1

SDL_Surface* instruction2 = NULL;

 

// Instruction Font 2

SDL_Surface* instruction3 = NULL;

 

// Instruction Font 3

SDL_Event

 

event; // Event Handling

TTF_Font* font = NULL;

SDL_Color fontColor = {255, 255, 255};

Sound* soundMgr = NULL;

//===================================================================//

// FUNCTION DECLARATION //

//===================================================================//

bool

 

 

initSDL(); // Initiliaze the SDL

 

 

// Application

bool

 

 

initTTF(); // Initialize True Type Fonts

bool

 

 

loadFiles(); // Load the files

SDL_Surface* loadBMP(string filename);

 

// Load the bitmaps and store

 

 

// them in an SDL Surface.

void

 

 

applySurface(int x, int y, // Apply the surface to the

SDL_Surface* src,

 

// video surface.

SDL_Surface* dest,

SDL_Rect* clip = NULL);

void

 

 

setText(); // Set up the text for each

 

 

// command.

void

 

 

renderText(); // Renders the text to the

 

 

// screen.

void

 

 

shutDown(); // Free the surfaces memory and

 

 

// shutdown the application.

//===================================================================//

int

 

 

main(int argc, char** argv)

{

 

 

bool quit = false;

 

 

if(!initSDL())

 

 

return -1;

 

 

if(!loadFiles())

 

 

return -1;

 

 

if(!soundMgr->initSound())

 

 

return -1;

setText();

applySurface(0, 0, background, videoSurface, 0);

renderText();

 

 

while(!quit)

{

 

 

if(SDL_PollEvent(&event))

{

 

 

if(event.type == SDL_KEYDOWN)

{

 

 

if(event.key.keysym.sym == SDLK_1)

{

soundMgr->playSound(1);

}

 

 

 

if(event.key.keysym.sym == SDLK_2)

{

soundMgr->playSound(2);

}

 

 

if(event.key.keysym.sym == SDLK_3)

{

soundMgr->playSound(3);

}

 

 

if(event.key.keysym.sym == SDLK_4)

{

soundMgr->playSound(4);

}

 

 

if(event.key.keysym.sym == SDLK_9)

{

soundMgr->pauseSound();

}

 

 

if(event.key.keysym.sym == SDLK_0)

{

soundMgr->stopSound();

}

}

 

 

else if(event.type == SDL_QUIT)

{

quit =

 

true;

}

}

 

 

if(SDL_Flip(videoSurface) == -1)

{

 

 

return -1;

}

}

shutDown();

 

 

return 0;

}

bool

 

 

initSDL()

{

 

 

if(SDL_Init(SDL_INIT_EVERYTHING) == -1)

 

 

return false;

videoSurface = SDL_SetVideoMode(SCRN_WIDTH, SCRN_HEIGHT, SCRN_BPP,

SDL_SWSURFACE);

 

 

if(videoSurface == NULL)

 

 

return false;

 

 

if(!initTTF())

 

 

return false;

SDL_WM_SetCaption(

 

"Playing Sound", NULL);

 

 

return true;

}

bool

 

 

loadFiles()

{

background = loadBMP(

 

"background.bmp");

 

 

if(background == NULL)

 

 

return false;

 

 

if(!soundMgr->loadFile())

 

 

return false;

font = TTF_OpenFont(

 

"robotic.ttf", 24);

 

 

if(font == NULL)

 

 

return false;

 

 

return true;

}

bool

 

 

initTTF()

{

 

 

if(TTF_Init() == -1)

 

 

return false;

 

 

return true;

}

SDL_Surface* loadBMP(string filename)

{

SDL_Surface* tempSurface = NULL;

SDL_Surface* optimizedImage = NULL;

tempSurface = SDL_LoadBMP(filename.c_str());

 

 

if(tempSurface != NULL)

{

optimizedImage = SDL_DisplayFormat(tempSurface);

SDL_FreeSurface(tempSurface);

 

 

if(optimizedImage != NULL)

{

Uint32 color = SDL_MapRGB(optimizedImage->format,

0, 0xFF, 0xFF);

SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, color);

}

}

 

 

return optimizedImage;

}

void

 

 

applySurface(int x, int y, SDL_Surface* src, SDL_Surface* dest,

SDL_Rect* clip)

{

SDL_Rect offset;

offset.x = x;

offset.y = y;

SDL_BlitSurface(src, clip, dest, &offset);

}

void

 

 

setText()

{

instruction1 = TTF_RenderText_Solid(font,

 

 

"Press 1, 2, 3, or 4 to play a sound effect", fontColor);

instruction2 = TTF_RenderText_Solid(font,

 

 

"Press 9 to play or pause the music", fontColor);

instruction3 = TTF_RenderText_Solid(font,

 

 

"Press 0 to stop the music", fontColor);

}

void

 

 

renderText()

{

 

 

// Apply the first set of instructions

applySurface((SCRN_WIDTH/2) - (instruction1->w/2),

(SCRN_HEIGHT/2)/2,

instruction1, videoSurface);

 

 

// Apply the second set of instructions

applySurface((SCRN_WIDTH/2) - (instruction2->w/2),

(SCRN_HEIGHT/2),

instruction2, videoSurface);

 

 

// Apply the last set of instructions

applySurface((SCRN_WIDTH/2) - (instruction2->w/2),

(SCRN_HEIGHT/2)/2 + (SCRN_HEIGHT/2),

instruction3, videoSurface);

}

void

 

 

shutDown()

{

SDL_FreeSurface(background);

SDL_FreeSurface(instruction1);

SDL_FreeSurface(instruction2);

SDL_FreeSurface(instruction3);

soundMgr->shutDown();

TTF_CloseFont(font);

TTF_Quit();

SDL_Quit();

}


PARTNERS