Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: ".SetSize (x, y)" acting strangely  (Read 5556 times)

0 Members and 1 Guest are viewing this topic.

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
".SetSize (x, y)" acting strangely
« on: July 27, 2011, 06:04:00 am »
The window resizing that is supposed to happen at lines 191-207 (right near the bottom) does nothing, and when I compile this on my ubuntu laptop, none of the window resizing does anything.  Did I do something wrong?

Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Go.h"


int main()
{
    // The zoom multiplyer, default : 1
    int zoomLevel = 1;

    // Load the spritesheet
    sf::Image spriteSheet;
    if (!spriteSheet.LoadFromFile("Go_sprites.png"))
    {
        // Error
    }
    // Get rid of the blurring effect
    spriteSheet.SetSmooth(false);
    // Make magenta see-through
    spriteSheet.CreateMaskFromColor(sf::Color::Magenta);

    // Load the font
    sf::Font myFont;
    if (!myFont.LoadFromFile ("6x13font.ttf", 36))
    {
        // Error
    }

        // Create main window
    sf::RenderWindow App(sf::VideoMode(245 * zoomLevel, 129 * zoomLevel),
                         "Go",
                         sf::Style::Titlebar | sf::Style::Close);

    // Reduce CPU usage by setting a max fps
    App.SetFramerateLimit(30);

    // TEMPORARY, Set a board size for testing
    int boardXDim = 9;
    int boardYDim = 9;

    // The main program loop
    // Closes when the window is closed
    while (App.IsOpened())
    {

        // Put all the menu stuff here

        Board myBoard (App, myFont, spriteSheet, boardXDim, boardYDim, zoomLevel);
        // The board loop
        // Exits when the window is closed or the game is exited
        while ((myBoard.inGame == true) && (App.IsOpened ()))
        {

            // Process the events list
            sf::Event Event;
            while (App.GetEvent (Event) && myBoard.inGame == true && App.IsOpened ())
            {
                // Close window : exit
                if (Event.Type == sf::Event::Closed)
                {
                    App.Close ();
                }

                // If the left mouse button was pressed
                if ((Event.Type == sf::Event::MouseButtonPressed) && (Event.MouseButton.Button == sf::Mouse::Left))
                {
                    // Clear the alerts
                    myBoard.clearAlerts ();

                    // if the mouse's x and y co-ordinates are on the pass button
                    if (myBoard.onPassButton ((((Event.MouseButton.X + 1) / zoomLevel) - 1), (((Event.MouseButton.Y + 1) / zoomLevel) - 1)))
                    {
                        myBoard.pass ();

                    }
                    // if the mouse's x and y co-ordinates are on a space on the board
                    if ((((((((Event.MouseButton.X + 1) / zoomLevel) - 1) - 1) % 14) > 0) && ((((Event.MouseButton.X + 1) / zoomLevel) - 1) <= (myBoard.gridToPixel (myBoard.xMax) + 6))) && (((((((Event.MouseButton.Y + 1) / zoomLevel) - 1) - 1) % 14) > 0) && ((((Event.MouseButton.Y + 1) / zoomLevel) - 1) <= (myBoard.gridToPixel (myBoard.yMax) + 6))))
                    {
                        // If the piece is sucessfully placed at the co-ordinates
                        if (myBoard.placePiece (myBoard.pixelToGrid (((Event.MouseButton.X + 1) / zoomLevel) - 1), myBoard.pixelToGrid (Event.MouseButton.Y / zoomLevel)))
                        {
                            // Move the selector to where the piece was placed
                            myBoard.selectorX = myBoard.pixelToGrid (((Event.MouseButton.X + 1) / zoomLevel) - 1);
                            myBoard.selectorY = myBoard.pixelToGrid (((Event.MouseButton.Y + 1) / zoomLevel) - 1);
                        }
                    }
                }

                // If a button was pressed act depending on which button
                if (Event.Type == sf::Event::KeyPressed)
                {
                    // If the key was escape, close the window
                    if (Event.Key.Code == sf::Key::Escape)
                    {
                        App.Close ();
                    }

                    // If the key was an arrow key ,vim key, or wasd key, move the selector accordingly
                    if ((Event.Key.Code == sf::Key::K) || (Event.Key.Code == sf::Key::Up) || (Event.Key.Code == sf::Key::W))
                    {
                        myBoard.moveSelector (0, -1);
                        myBoard.clearAlerts ();
                    }
                    if ((Event.Key.Code == sf::Key::H) || (Event.Key.Code == sf::Key::Left) || (Event.Key.Code == sf::Key::A))
                    {
                        myBoard.moveSelector (-1, 0);
                        myBoard.clearAlerts ();
                    }
                    if ((Event.Key.Code == sf::Key::L) || (Event.Key.Code == sf::Key::Right) || (Event.Key.Code == sf::Key::D))
                    {
                        myBoard.moveSelector (1, 0);
                        myBoard.clearAlerts ();
                    }
                    if ((Event.Key.Code == sf::Key::J) || (Event.Key.Code == sf::Key::Down) || (Event.Key.Code == sf::Key::S))
                    {
                        myBoard.moveSelector (0, 1);
                        myBoard.clearAlerts ();
                    }

                    // If space or enter was pressed, try to place a piece
                    if ((Event.Key.Code == sf::Key::Space) || (Event.Key.Code == sf::Key::Return))
                    {
                        myBoard.placePiece (myBoard.selectorX, myBoard.selectorY);
                    }

                    // If 'p' was pressed, pass
                    if (Event.Key.Code == sf::Key::P)
                    {
                        myBoard.pass ();
                        myBoard.clearAlerts ();
                    }

                    // If 1 - 3 was pressed change the zoom level
                    if (Event.Key.Code == '1')
                    {
                        zoomLevel = 1;
                        App.SetSize (myBoard.smallWindowWidth * zoomLevel, myBoard.smallWindowHeight * zoomLevel);
                    }

                    if (Event.Key.Code == '2')
                    {
                        zoomLevel = 2;
                        App.SetSize (myBoard.smallWindowWidth * zoomLevel, myBoard.smallWindowHeight * zoomLevel);
                    }

                    if (Event.Key.Code == '3')
                    {
                        zoomLevel = 3;
                        App.SetSize (myBoard.smallWindowWidth * zoomLevel, myBoard.smallWindowHeight * zoomLevel);
                    }
                }
            }
            // If the game is over begin the final counting of points
            if (myBoard.inGame == false)
            {
                // Start the wrap-up phase
                myBoard.inWrapUp = true;

                // Count surrounded space, add markers, update marker strings, and update the alerts string to who won and what to do next
                myBoard.countSpace ();

                // Continues to display until the window is closed, escape is pressed, or enter is pressed
                while ((myBoard.inWrapUp == true) && App.IsOpened ())
                {
                    // Process events for the wrap-up phase
                    sf::Event newEvent;
                    while (App.GetEvent (newEvent) && myBoard.inWrapUp == true && App.IsOpened ())
                    {
                        // Close window : exit
                        if (newEvent.Type == sf::Event::Closed)
                        {
                            App.Close ();
                        }

                        // If a button was pressed
                        if (newEvent.Type == sf::Event::KeyPressed)
                        {
                            // If the escape button was pressed exit the game
                            if (newEvent.Key.Code == sf::Key::Escape)
                            {
                                App.Close ();
                            }

                            // If the enter button was pressed, exit the wrap-up phase
                            if (newEvent.Key.Code == sf::Key::Return)
                            {
                                myBoard.inWrapUp = false;
                            }

                             // If 1 - 3 was pressed change the zoom level
                            if (Event.Key.Code == '1')
                            {
                                zoomLevel = 1;
                                App.SetSize (myBoard.smallWindowWidth * zoomLevel, myBoard.smallWindowHeight * zoomLevel);
                            }

                            if (Event.Key.Code == '2')
                            {
                                zoomLevel = 2;
                                App.SetSize (myBoard.smallWindowWidth * zoomLevel, myBoard.smallWindowHeight * zoomLevel);
                            }

                            if (Event.Key.Code == '3')
                            {
                                zoomLevel = 3;
                                App.SetSize (myBoard.smallWindowWidth * zoomLevel, myBoard.smallWindowHeight * zoomLevel);
                            }
                        }
                    }

                    // Draw and display the board
                    myBoard.drawBoard ();
                }
            }

            // Draw and display the board
            myBoard.drawBoard ();
        }
    }
    return 0;
}

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
".SetSize (x, y)" acting strangely
« Reply #1 on: July 27, 2011, 06:41:45 am »
PLEASE remove EVERYTHING that is not related to the problem you're having.
I use the latest build of SFML2

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
".SetSize (x, y)" acting strangely
« Reply #2 on: July 27, 2011, 07:47:30 am »
I thought it would be better to give too much information than to accidentally cut out the problem.  Is there some standard procedure to find out what is excess?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
".SetSize (x, y)" acting strangely
« Reply #3 on: July 27, 2011, 07:53:54 am »
Accidentally cutting out the problem is not a bad thing, it means that you've found the source of the problem ;)

I suggest that you remove lines one by one until the problem disappears. But if SetSize is not working... you probably don't need more than the calls to SetSize. And if SetSize alone works, then you have a problem in your code and you'll most likely find the guilty line(s) while working on the minimal code.
Laurent Gomila - SFML developer

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
".SetSize (x, y)" acting strangely
« Reply #4 on: July 27, 2011, 06:47:12 pm »
Okay, it is now as small as it can be without making the problem moot, but the problem is still there.  Where did I go wrong?

Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    int zoomLevel = 1;

    sf::RenderWindow App(sf::VideoMode(300, 200),
                         "Go",
                         sf::Style::Titlebar | sf::Style::Close);

    while (App.IsOpened())
    {
        bool inGame = true;
        bool inWrapUp = false;
        int smallWindowHeight = 200;
        int smallWindowWidth = 300;

        while ((inGame == true) && (App.IsOpened ()))
        {
            sf::Event Event;
            while (App.GetEvent (Event) && inGame == true && App.IsOpened ())
            {
                if (Event.Type == sf::Event::Closed)
                {
                    App.Close ();
                }

                if (Event.Type == sf::Event::KeyPressed)
                {

                    if (Event.Key.Code == sf::Key::P)
                    {
                        inGame = false;
                    }

                    if (Event.Key.Code == '1')
                    {
                        zoomLevel = 1;
                        App.SetSize (smallWindowWidth * zoomLevel, smallWindowHeight * zoomLevel);
                    }

                    if (Event.Key.Code == '2')
                    {
                        zoomLevel = 2;
                        App.SetSize (smallWindowWidth * zoomLevel, smallWindowHeight * zoomLevel);
                    }

                    if (Event.Key.Code == '3')
                    {
                        zoomLevel = 3;
                        App.SetSize (smallWindowWidth * zoomLevel, smallWindowHeight * zoomLevel);
                    }
                }
            }

            if (inGame == false)
            {

                inWrapUp = true;

                while ((inWrapUp == true) && App.IsOpened ())
                {

                    sf::Event newEvent;
                    while (App.GetEvent (newEvent) && inWrapUp == true && App.IsOpened ())
                    {

                        if (newEvent.Type == sf::Event::Closed)
                        {
                            App.Close ();
                        }

                        if (newEvent.Type == sf::Event::KeyPressed)
                        {

                            if (newEvent.Key.Code == sf::Key::Return)
                            {
                                inWrapUp = false;
                            }

                            if (Event.Key.Code == '1')
                            {
                                zoomLevel = 1;
                                App.SetSize (smallWindowWidth * zoomLevel, smallWindowHeight * zoomLevel);
                            }

                            if (Event.Key.Code == '2')
                            {
                                zoomLevel = 2;
                                App.SetSize (smallWindowWidth * zoomLevel, smallWindowHeight * zoomLevel);
                            }

                            if (Event.Key.Code == '3')
                            {
                                zoomLevel = 3;
                                App.SetSize (smallWindowWidth * zoomLevel, smallWindowHeight * zoomLevel);
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
".SetSize (x, y)" acting strangely
« Reply #5 on: July 27, 2011, 07:28:57 pm »
Your code is still far too big. Especially since you don't tell us where exactly the error occurs.

You duplicate a lot of code (differentiating key codes '1', '2', '3', almost the same handling for inGame == true and inGame == false). You can minimize a lot, even without losing functionality. Don't write the same code more than once, use functions to outsource often-needed code.

For the thread here, just provide a minimal complete example still reproducing the problem. For that, all the advanced game logic and the exact key codes are irrelevant.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
".SetSize (x, y)" acting strangely
« Reply #6 on: July 28, 2011, 03:19:25 am »
I found the problem.  I checked the keycode of Event instead of newEvent for part of the second event catching loop.

Also, should I condense both of the event catching loops into one loop with some "if" statements for gamestage specific controls?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
".SetSize (x, y)" acting strangely
« Reply #7 on: July 28, 2011, 10:59:47 am »
Quote from: "blueeyedlion"
Also, should I condense both of the event catching loops into one loop with some "if" statements for gamestage specific controls?
I would do that, the code is very similar. You could also use a separate function.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: