migcak's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
36 (0.05 per day)
Joined:
15-August 12
Profile Views:
458
Last Active:
User is offline Jul 26 2014 08:38 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Playstation
Your Car:
Dodge
Dream Kudos:
0
Icon   migcak Feeling optimistic

Posts I've Made

  1. In Topic: Problem using SDL_FreeSurface

    Posted 23 Jul 2014

    View PostPeter O, on 23 July 2014 - 01:05 AM, said:

    I hope you check if the surface passed to apply_surface is null before passing it to SDL_BlitSurface.

    If you don't draw anything on top of where you drew the button it will still show even if you stop drawing it. You probably should draw the background just before calling myButton.show();

    I am checking to see if the surfaces are null. I have a loading function that returns false if the surface is null
    bool load_files()
    {
    	background = load_image("Lightbackground.png");
    
    	board = load_image("tic-tac-toe-board.png");
    
    	button = load_image("Fplaybutton.bmp");
    
    	font = TTF_OpenFont("lazy.ttf", 30);
    
    	if(background == NULL)
    		return false;
    
    	if(board == NULL)
    		return false;
    
    	if(button == NULL)
    		return false;
    
    	if(font == NULL)
    		return false;
    
    	return true;
    }
    


    I tried to redraw the background right before myButton.show() but that didn't quite work, though I used your idea and put it somewhere else and it did work. I redrew the board right before I put the game board on the screen in handle_input().

    here's what it looks like

    void Button::handle_input()
    {
    	int x = 0, y = 0;
    
    	if(event.type == SDL_MOUSEBUTTONDOWN)
    	{
    		if(event.button.button == SDL_BUTTON_LEFT)
    		{
    			x = event.button.x;
    			y = event.button.y;
    		}
    
    			if((x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h))
    			{
    				log("Message delete");
    				if(message != NULL)
    					SDL_FreeSurface(message);
    
    				message = NULL;
    
    				log("Button delete");
    				if(button != NULL)
    					SDL_FreeSurface(button);
    
    				button = NULL;
    
                    //placing it here seems to work
    				apply_surface(0, 0, background, screen); 
    
    				log("Apply game board");
    				apply_surface(0, 0, board, screen);
    			}
    	}
    }
    
    void Button::show()
    {
    	apply_surface(box.x, box.y, button, screen);
    }
    
    int main(int argc, char* args[])
    {
    	bool quit = false;
    
    	Button myButton(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 200, 125);
    
    	if(init() == false)
    		return 1;
    
    	if(load_files() == false)
    		return 1;
    
    	message = TTF_RenderText_Solid(font, "Welcome to George's Tic Tac Toe Game", textColor);
    
    	if(message == NULL)
    		return 1;
    
    	apply_surface(0, 0, background, screen);
    	apply_surface(75, 150, message, screen);
    
    	while(quit == false)
    	{
    		if(SDL_PollEvent(&event))
    		{
    			log("Calling handle_input");
    			myButton.handle_input();
    
    			if(event.type == SDL_QUIT)
    				quit = true;
    		}
    
    		myButton.show();
    
    		if(SDL_Flip(screen) == -1)
    			return 1;
    	}
    
    	clean_up();
    
    	return 0;
    }
    
  2. In Topic: Problem using SDL_FreeSurface

    Posted 22 Jul 2014

    I changed my code like it was suggested but it still has issues. The game doesn't freeze right now, but the message and the button are still around when it should be deleted. The Tic-Tac-Toe game board does show up like I want. I tried rearranging where I called apply_surface(0, 0, background, screen) and apply_surface(75, 150, message, screen) in my main function but the game board wouldn't show up.

    I also made a game log to keep track of what is happening when I call handle_input() and deleting the message and button as well as applying the Tic-Tac-Toe game board. Here is the text file Attached File  log.txt (3.08K)
    Number of downloads: 4


    Here is some more code (and old code) that should be useful for the problem.
    #include "SDL.h"
    #include "SDL_image.h"
    #include "SDL_ttf.h"
    #include <string>
    #include <fstream>
    using namespace std;
    
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 520;
    const int SCREEN_BPP = 32;
    
    SDL_Surface *background = NULL;
    SDL_Surface *message = NULL;
    SDL_Surface *button = NULL;
    SDL_Surface *board = NULL;
    SDL_Surface *screen = NULL;
    
    SDL_Color textColor = {0, 0, 0};
    
    TTF_Font *font = NULL;
    
    SDL_Event event;
    
    ofstream logger("log.txt");
    
    void log(string message)
    {
    	logger << message << endl;
    
    	logger.flush();
    }
    
    //More functions here but I don't think they have anything to do with this problem 
    
    void Button::handle_input()
    {
    	int x = 0, y = 0;
    
    	if(event.type == SDL_MOUSEBUTTONDOWN)
    	{
    		if(event.button.button == SDL_BUTTON_LEFT)
    		{
    			x = event.button.x;
    			y = event.button.y;
    		}
    
    			if((x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h))
    			{
    				log("Message delete");
    				if(message != NULL)
    					SDL_FreeSurface(message);
    
    				message = NULL;
    
    				log("Button delete");
    				if(button != NULL)
    					SDL_FreeSurface(button);
    
    				button = NULL;
    
    				log("Apply game board");
    				apply_surface(0, 0, board, screen);
    			}
    	}
    }
    
    void Button::show()
    {
    	apply_surface(box.x, box.y, button, screen);
    }
    
    int main(int argc, char* args[])
    {
    	bool quit = false;
    
    	Button myButton(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 200, 125);
    
    	if(init() == false)
    		return 1;
    
    	if(load_files() == false)
    		return 1;
    
    	message = TTF_RenderText_Solid(font, "Welcome to my Tic Tac Toe Game", textColor);
    
    	if(message == NULL)
    		return 1;
    
    	apply_surface(0, 0, background, screen);
    	apply_surface(75, 150, message, screen);
    
    
    	while(quit == false)
    	{
    		if(SDL_PollEvent(&event))
    		{
    			log("Calling handle_input");
    			myButton.handle_input();
    
    			if(event.type == SDL_QUIT)
    				quit = true;
    		}
    
    		myButton.show();
    
    		if(SDL_Flip(screen) == -1)
    			return 1;
    	}
    
    	clean_up();
    
    	return 0;
    }
    
    
  3. In Topic: SDL Help with Handling events and timer

    Posted 20 May 2014

    I agree you only need the else statement and I put the if with the else statement for showing it here for help. I also see your point using Boolean variables as the original code that taught me about playing sound had it. But the problem would still be happening even with these changes, I believe anyway. After 10 seconds the program doesn't play the sound like I want. The timer keeps going after 10 seconds. Unless you're saying because I'm calling Mix_PlayChannel without Boolean variables that is the reason the program won't call Mix_PlayChannel after 10 seconds.
  4. In Topic: SDL Help with Handling events and timer

    Posted 20 May 2014

    But shouldn't start change because I want to wait 10 seconds. And start is the variable that I compare 10 seconds to.
  5. In Topic: SDL Help with Handling events and timer

    Posted 18 May 2014

    To make start change don't I need another Uint32 variable to go along with it?
    For example,
    old = start;
    start = SDL_GetTicks();
    The order of my two inner if statements need to be swapped I think too. I think I should check if the current time is less than 10 seconds and if it is then I should check if there is a keyboard being pressed. I can then allow the player to press the space bar but if 10 seconds already then I can play the sound. Also, time shouldn't change because I want to see if its been 10 seconds and then play a sound. I put that if statement so I could make sure the logic was right(I wanted to reread it to make sure that the code does what I wanted it to) after I tested the program.

My Information

Member Title:
New D.I.C Head
Age:
21 years old
Birthday:
February 23, 1993
Gender:
Location:
Tennessee
Interests:
Artificial Intelligence
C++ programing
game programming
Years Programming:
2
Programming Languages:
C++
C#
Python
LUA

Contact Information

E-mail:
Click here to e-mail me
Yahoo:
Yahoo  george_hunter86@yahoo.com

Friends

Comments

migcak has no profile comments yet. Why not say hello?