ui_button.cpp

The UIButton object has several functions that must be defined. We have created a new ui_button.cpp file that will hold all of these new functions. We will need to define a constructor, as well asĀ MouseMove, MouseClick, MouseUp, KeyDown, and RenderUI.

First, we will include our game.hpp file:

#include "game.hpp"

Now, we will define our constructor function:

UIButton::UIButton( int x, int y, char* file_name, char* hover_file_name, char* click_file_name, void (*callback)() ) {
m_Callback = callback;
m_dest.x = x;
m_dest.y = y;
SDL_Surface *temp_surface = IMG_Load( file_name );

if( !temp_surface ) {
printf("failed to load image: %s\n", IMG_GetError() );
return;
}
else {
printf("success creating ui button surface\n");
}
m_SpriteTexture = SDL_CreateTextureFromSurface( renderer,
temp_surface );
if( !m_SpriteTexture ) {
return;
}
SDL_QueryTexture( m_SpriteTexture,
NULL, NULL,
&m_dest.w, &m_dest.h );
SDL_FreeSurface( temp_surface );

temp_surface = IMG_Load( click_file_name );
if( !temp_surface ) {
printf("failed to load image: %s\n", IMG_GetError() );
return;
}
else {
printf("success creating ui button click surface\n");
}
m_ClickTexture = SDL_CreateTextureFromSurface( renderer,
temp_surface );

if( !m_ClickTexture ) {
return;
}
SDL_FreeSurface( temp_surface );

temp_surface = IMG_Load( hover_file_name );
if( !temp_surface ) {
printf("failed to load image: %s\n", IMG_GetError() );
return;
}
else {
printf("success creating ui button hover surface\n");
}
m_HoverTexture = SDL_CreateTextureFromSurface( renderer,
temp_surface );

if( !m_HoverTexture ) {
return;
}
SDL_FreeSurface( temp_surface );

m_dest.x -= m_dest.w / 2;
m_dest.y -= m_dest.h / 2;

m_Hover = false;
m_Click = false;
m_Active = true;
}

The constructor function starts by setting the callback function from the passed in parameter:

m_Callback = callback;

Then, it sets the m_dest rectangle's x and y coordinates from the parameters we passed in:

m_dest.x = x;
m_dest.y = y;

After that, it loads three different image files into three different textures for the button, the button's hover state, and the button's clicked state:

SDL_Surface *temp_surface = IMG_Load( file_name );

if( !temp_surface ) {
printf("failed to load image: %s\n", IMG_GetError() );
return;
}
else {
printf("success creating ui button surface\n");
}
m_SpriteTexture = SDL_CreateTextureFromSurface( renderer, temp_surface );

if( !m_SpriteTexture ) {
return;
}
SDL_QueryTexture( m_SpriteTexture,
NULL, NULL,
&m_dest.w, &m_dest.h );
SDL_FreeSurface( temp_surface );

temp_surface = IMG_Load( click_file_name );

if( !temp_surface ) {
printf("failed to load image: %s\n", IMG_GetError() );
return;
}
else {
printf("success creating ui button click surface\n");
}
m_ClickTexture = SDL_CreateTextureFromSurface( renderer, temp_surface );

if( !m_ClickTexture ) {
return;
}
SDL_FreeSurface( temp_surface );

temp_surface = IMG_Load( hover_file_name );
if( !temp_surface ) {
printf("failed to load image: %s\n", IMG_GetError() );
return;
}
else {
printf("success creating ui button hover surface\n");
}
m_HoverTexture = SDL_CreateTextureFromSurface( renderer, temp_surface );

if( !m_HoverTexture ) {
return;
}
SDL_FreeSurface( temp_surface );

The preceding code should look pretty familiar because loading an image file into an SDL_Texture object is something we have done a lot at this point. After that, we use the width and height values we queried earlier to center the destination rectangle:

m_dest.x -= m_dest.w / 2;
m_dest.y -= m_dest.h / 2;

Then, we set our hover, click, and active state flags:

m_Hover = false;
m_Click = false;
m_Active = true;