Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Some help with SDL in C++ fade in and fade out


Jacquelinett
 Share

Recommended Posts

Alright im having this weird problem. All the code work fine, but take a look at the code, specificly at the Options.ScreenSize. My current value is 1\. And that make my screensize 800x600\. The fade in and fade out code work fine. The picture Opening1 and Opening2 as well as Opening3 do appear and fade as they supposed to, everything is fine, but… until you change the screensize to 0 and 2

When the screensize is 0 or 2\. The Opening1 and 2 still work, but, it doesnt fade in and out. It just simply change (like it suppose to) when it suppose to. No fade in or fade out happen. And I do not know why.

All the code here is RIGHT. Which mean, I have no undefined mistake, I have several header that contain the definition of these thing, but I only put my main.cpp right here:

>! ```
#include "FirstHeader.h"
#include "Declaration.h"
>! using namespace std;
>! class Buttons
{
}
>! 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();
};
>! void Winsock_Init()
{
long answer;
WSAData wsaData;
WORD DLLVersion;
DLLVersion = MAKEWORD(2,1);
answer = WSAStartup(DLLVersion, &wsaData);
>! SOCKADDR_IN addr;
>! int addrlen = sizeof(addr);
>! SOCKET sConnect;
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
>! addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_family = AF_INET;
addr.sin_port = htons(8001);
}
>! struct Options
{
const char* GameName;
int ScreenSize;
} Options;
>! void GetScreenSize(int *width, int *height, int mode)
{
  if (width == NULL || height == NULL)
    return;

  switch (mode)
  {
    case 1:
    {
      *width  = 800;
      *height = 600;
    }
    break;

    case 2:
    {
      *width  = 1024;
      *height = 768;
    }
    break;

    default:
    {
      *width  = 640;
      *height = 480;
    }
    break;
  }
}
>! void SetUpOpening(int mode)
{
switch (mode)
{
case 1:
{
Opening1 = IMG_Load("Graphics/Opening/1/1.png");
PureBlackScreen = IMG_Load("Graphics/Opening/1/2.png");
Opening2 = IMG_Load("Graphics/Opening/1/3.png");
Opening3 = IMG_Load("Graphics/Opening/1/4.png");
}
break;
>! case 2:
{
Opening1 = IMG_Load("Graphics/Opening/2/1.png");
PureBlackScreen = IMG_Load("Graphics/Opening/2/2.png");
Opening2 = IMG_Load("Graphics/Opening/2/3.png");
Opening3 = IMG_Load("Graphics/Opening/2/4.png");
}
break;

default:
{
Opening1 = IMG_Load("Graphics/Opening/0/1.png");
PureBlackScreen = IMG_Load("Graphics/Opening/0/2.png");
Opening2 = IMG_Load("Graphics/Opening/0/3.png");
Opening3 = IMG_Load("Graphics/Opening/0/4.png");
}
break;
}
}
>! void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;
>!     //Get offsets
    offset.x = x;
    offset.y = y;
>!     //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}
>! void clean_up()
{
    //Free the image
    //SDL_FreeSurface( screen );
>! SDL_FreeSurface(PureBlackScreen);
SDL_FreeSurface(Opening3);
SDL_FreeSurface(Opening1);
SDL_FreeSurface(Opening2);

    //Quit SDL
    SDL_Quit();   
}
>! bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;   
    }
>! int Width;
int Height;
    GetScreenSize(&Width, &Height, Options.ScreenSize);
SetUpOpening(Options.ScreenSize);
screen = SDL_SetVideoMode(Width, Height, 32, SDL_SWSURFACE);
apply_surface(0,0,Opening1, screen);
>! if(Opening1 == NULL)
{
return false;
}
>!     //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;   
    }

    //Set the window caption
SDL_WM_SetCaption( Options.GameName, NULL );

    //If everything initialized fine
    return true;
}
>! 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;
}
>! 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;
    }
}
>! 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;
}
>! bool Timer::is_started()
{
    return started;
}
>! bool Timer::is_paused()
{
    return paused;
}
>! int main(int argc, char* args[])
{
//Start Winsock
Winsock_Init();
>! SDL_Surface* hello = NULL;
>! bool Quit = false;
OpeningScene1 = false;
OpeningScene2 = true;
OpeningScene3 = true;
OpeningScene4 = true;
const int FRAMES_PER_SECOND = 20;
int alpha = SDL_ALPHA_OPAQUE;
Timer fps;
>! Options.GameName = "My Little Kingdom";
Options.ScreenSize = 1;
>! //Initialize
    if( init() == false )
    {
        return 1;   
    }
>!     SDL_Flip(screen);
SDL_Delay(2000);
>!     //Quit SDL
    while (Quit == false)
{
fps.start();
>! while ( SDL_PollEvent(&event))
{
if ( event.type == SDL_QUIT)
{
Quit = true;
}
}
//If alpha is not at minimum
while(OpeningScene1 == false)
        {
while( alpha > SDL_ALPHA_TRANSPARENT )
{
//Decrease alpha
alpha -= 5;
>! SDL_SetAlpha( Opening1, SDL_SRCALPHA, alpha );

//Apply the back
apply_surface( 0, 0, PureBlackScreen, screen, NULL );
apply_surface( 0, 0, Opening1, screen, NULL );
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;   
}
if(alpha == SDL_ALPHA_TRANSPARENT)
{
OpeningScene2 = false;
OpeningScene1 = true;

}
}
        }
while (OpeningScene2 == false)
{
while (alpha < SDL_ALPHA_OPAQUE)
{
alpha += 5;
SDL_SetAlpha(Opening2, SDL_SRCALPHA, alpha);
apply_surface(0,0, PureBlackScreen, screen, NULL);
apply_surface(0,0, Opening2, screen, NULL);
if( SDL_Flip( screen ) == -1 )
{
return 1;   
}
if (alpha == SDL_ALPHA_OPAQUE)
{
OpeningScene3 = false;
OpeningScene2 = true;
SDL_Delay(2000);
}
}
}
while(OpeningScene3 == false)
        {
while( alpha > SDL_ALPHA_TRANSPARENT )
{
//Decrease alpha
alpha -= 5;
>! SDL_SetAlpha( Opening2, SDL_SRCALPHA, alpha );

//Apply the back
apply_surface( 0, 0, PureBlackScreen, screen, NULL );
apply_surface( 0, 0, Opening2, screen, NULL );
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;   
}
if(alpha == SDL_ALPHA_TRANSPARENT)
{
OpeningScene3 = true;
OpeningScene4 = false;
}
}
        }
while (OpeningScene4 == false)
{
while (alpha < SDL_ALPHA_OPAQUE)
{
alpha += 5;
SDL_SetAlpha(Opening3, SDL_SRCALPHA, alpha);
apply_surface(0,0, PureBlackScreen, screen, NULL);
apply_surface(0,0, Opening3, screen, NULL);
if( SDL_Flip( screen ) == -1 )
{
return 1;   
}
if (alpha == SDL_ALPHA_OPAQUE)
{
OpeningScene4 = true;
}
}
}
}
>! clean_up();
return 0;
}
```
Link to comment
Share on other sites

Well it would be nice to actually go through all the code. Not trying to be a douchebag or rip your card apart but here is some random stuff i saw. Again this is just my personal opinion.

Might want to actually have Zero in your switch instead of just relying on the default. That should be an error check if anything. Also your pointers seem a bit weird though i would have to go through it more to see.

Might as well make your alpha changer into a function as well. You have almost identical code running 4 separate times. I think a good rule is if your going to use it more then twice make it a function.

Try to always declare your variables on creation if you can you initialize int width; then assign a value to it but just to be on the safe side try to always int width = 0;

Throw your timer class and other classes declare into there own separate header and cpp files, if you are trying for OOP that is.

Your naming conventions are also a tad mixed up. Most of the time you are using camel casing which is generally used in the industry. But then you switch to some variables that have caps in the beginning. While not a big deal it will save some time if you always follow the same formula. Also adding m in front of class variables ex: mWidth; is a good way to make sure that you always know its a member variable. Though that is preference. Same with const int FRAMES_PER_SECOND = 20; add a lowercase k in front of the constant variables. Also as for names with all capitals, those are usually reserved for DEFINES.

I suggest just setting some breakpoints and seeing exactly what happens when it runs as intended. Then switch the value and see what changes, then it should be pretty apparent what is happening. Check all values in the process.

Your timer Constructor looks like
```
Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

```
While this works you should generally:
```
Timer::Timer()
:startTicks(0)
,pausedTicks(0)
,paused(false)
,started(false)
{
  // Clear stuff like arrays here. Or things that require if statements.
}

```
your button class is missing a ;
```
class Buttons
{
}

```
Not sure what this is about. You dont need brackets around each statement.
```
    if( ( started == true ) && ( paused == false ) )

```
Crap that really doesnt matter

>! while (OpeningScene4 == true) can be changed to just while(OpeningScene4). Same with false while (!OpeningScene4 ). Though some people prefer == true for readability.

Upload your project or teamviewer me and i can look at it for you. Good job so far, wish i had that level of knowledge at your age.
Link to comment
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
 Share

×
×
  • Create New...