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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - ToanBao122

Pages: [1]
1
General / Re: Write a text when an object/or shape is clicked
« on: November 23, 2020, 02:22:28 am »
I suppose writing the text so it is shown on the screen. Here's what I try to do, when I click on the shape, I wasn't able to write anything:
Typing.h
#ifndef SFML_PROJECT_TYPING_H
#define SFML_PROJECT_TYPING_H

#include <SFML/Graphics.hpp>

class Typing: public sf::Text {
private:
    sf::Font font;
public:
    void addEvents(sf::RenderWindow& window, sf::Event event);
    Typing();
};
Typing.cpp
#include "Typing.h"
void Typing::addEvents(sf::RenderWindow& window, sf::Event event)
{
    while(window.pollEvent(event)){
        if(event.type == sf::Event::TextEntered)
        {
            setString(getString() + event.text.unicode);
        }
    }
}
Typing::Typing()
{
    font.loadFromFile("arial.ttf");
    setFont(font);
    setCharacterSize(12);
    setFillColor(sf::Color::Red);
}
task.h
#include <SFML/Graphics.hpp>
#include "Typing.h"
class task : public sf::Drawable, sf::Transformable {
private:
    Typing test;
    sf::Text text;
    sf::Font font;
    sf::RectangleShape background;

public:
    task();
    void setPosition(float x,float y);
    void setText();
    void setBackGround();
    void addEvent(sf::RenderWindow& window, sf::Event event);
    void draw(sf::RenderTarget &window, sf::RenderStates state) const;

};
task.cpp
void task::setText()
{
    sf::FloatRect bounds = background.getGlobalBounds();
    test.setPosition(bounds.left+20,bounds.top+30);
}
void task::setBackGround() {
    background.setSize({300.f,50.f});
    background.setFillColor(sf::Color::White);
}
task::task() {
    this->setBackGround();
}

void task::draw(sf::RenderTarget &window, sf::RenderStates state) const
{
    window.draw(background);
    window.draw(test);
}
void task::setPosition(float x, float y) {
    background.move(x,y);
    this->setText();
}
void task::addEvent(sf::RenderWindow& window, sf::Event event)
{  if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        sf::FloatRect bounds = background.getGlobalBounds();
        sf::Vector2f mpos = (sf::Vector2f)sf::Mouse::getPosition(window);
        if (bounds.contains(mpos))
        {
            test.addEvents(window,event);
        }
    }
}
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "task.h"
int main() {
sf::RenderWindow window(sf::VideoMode(1980, 1080, 32), "Test");
    Card card("Ace","Hearts");
    task task;
    Button test;
    test.setText("Add task");
    test.setPosition(400,300);
    TaskList theTest;
    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            } else
                task.addEvent(window,event);

        }
        window.clear(sf::Color::Black);
        window.draw(task);
        window.display();
    }
    return 0;
}

2
General / Write a text when an object/or shape is clicked
« on: November 22, 2020, 10:00:32 pm »
Hello, can anyone help me with this problem. I'm trying to set up something like when I click on an object(or a shape), I would be able to write something inside of that object/shape. Thank you!

3
Graphics / Re: Sprite fail to load when creating multiple objects
« on: November 07, 2020, 08:51:39 pm »
Anyway, I've fixed it by changing the setPosition in Box::setPosition to left and top instead of width and height.

4
Graphics / Re: Sprite fail to load when creating multiple objects
« on: November 07, 2020, 01:39:06 am »
To be more precise, Box test(1,1) (regardless whether which instances are declared first) is the only instance where the sprite is drawn, all the other instances of box just draw the rectangle shape and not the sprite.

5
Graphics / Re: Sprite fail to load when creating multiple objects
« on: November 06, 2020, 11:22:48 pm »
Well, as I said, the first intance of the class, the image is loaded properly, however for the subsequent instances, only the rectangle shape is drawn and not the sprite. Specifically, in the main.cpp file, "Box test(1,1)" is drawn properly with both the rectangleshape and the sprite, but for the "Box test1(2,2)" only the rectangle shape is drawn and not the sprite.

6
Graphics / Sprite fail to load when creating multiple objects
« on: November 06, 2020, 02:09:44 am »
Hi guys, I've encountered a problem when trying to draw objects, and I think that it might have something to do with sf::Sprite or sf::Texture, I'm not sure. Basically, my object consist of a rectangle shape and a sprite, when I try to draw it for the first object everything is exactly how I pictured it. However, the problem arise when I try to different objects of the same class. Specifically, for the subsequent objects drawn, only the rectangle shape was showing and not the sprite. How can I fix this problem? Here's my code:
Box.h
#ifndef LINKEDLIST_TEMPLATE_BOX_H
#define LINKEDLIST_TEMPLATE_BOX_H


#include <SFML/Graphics.hpp>

class Box:public sf::Drawable, sf::Transformable {
protected:
    int col;
    int row;
    sf::Color color;
    sf::RectangleShape boxBackGround;
    sf::Sprite queen;
    sf::Texture texture;
public:
   //sf::RectangleShape newBox(float x,float y);
    void draw(sf::RenderTarget &window, sf::RenderStates state) const;
    int getRow();
    int getCol();
    void setRow(int row);
    void setCol(int col);
    Box(int row, int col);
    void setBox(int x, int y);
    Box();
    void setTexture();
    void setQueen();
};


#endif //LINKEDLIST_TEMPLATE_BOX_H
Box.cpp
#include "Box.h"
//
// Created by baoto on 10/24/2020.
//

#include "Box.h"
int Box::getCol() {
    return col;
}
int Box::getRow() {
    return row;
}
void Box::setCol(int col) {
    this->col = col;
}
void Box::setRow(int row) {
    this->row = row;
}
Box::Box(){
    //this->setBox();
    row = 0;
    col = 0;
}
Box::Box(int row, int col) {
    this->row = row;
    this->col = col;
    this->setBox(this->row,this->col);
    this->setTexture();
    this->setQueen();

}
void Box::setBox(int x, int y) {
    boxBackGround.setSize({200.f,200.f});
    boxBackGround.setFillColor(sf::Color::White);
    boxBackGround.setPosition(x*210,y*210);
}
/*sf::RectangleShape Box::newBox(float x, float y)
{
    sf::RectangleShape s;
    s.setSize({200.f,200.f});
    s.setFillColor(sf::Color::White);
    s.setPosition(x,y);
    return s;
}*/

void Box::draw(sf::RenderTarget &window, sf::RenderStates state) const {
    window.draw(boxBackGround);
    window.draw(queen);
}
void Box::setTexture() {
    this->texture.loadFromFile("queen.png");
}
void Box::setQueen() {
    this->queen.setTexture(this->texture);
    this->queen.setScale(0.08f,0.08f);
    this->queen.setPosition(this->boxBackGround.getGlobalBounds().left+50,this->boxBackGround.getGlobalBounds().top+30);
}
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "CardGrid.h"
#include "NQueens.h"
int main() {

    Box test(1,1);
    Box test1(2,2);
    sf::RenderWindow window(sf::VideoMode(1980, 1080, 32), "Test");
    while(window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
        window.clear(sf::Color::Black);
        window.draw(test);
        window.draw(test1);
        window.display();
    }
return 0;

7
Graphics / Re: Help with drawing NQueens problem using 2D arrays of objects
« on: November 05, 2020, 10:05:46 pm »
The problem seems to in the following: Board::addQueen in Board.cpp; and when declare the Box at: "class Box:public sf::Drawable, sf::Transformable", I think.

8
Graphics / Help with drawing NQueens problem using 2D arrays of objects
« on: November 05, 2020, 02:14:40 am »
Hello everyone, I try to run a program to draw an empty chess board(each tile inside of the chess board is categorized as box) save for the queen piece at the position derived from the NQueens algorithm. My nqueens algorithm is correct, because I tested it, and It was able to give the correct position in the board(as in it was able to give me the correct rows, and columns of the queen). However, when I try to draw the whole thing, the window pop up in a white screen and close immediately without showing anything, while the terminal shows: "Process finished with exit code -1073741819 (0xC0000005)". Can you help me with this problem? Here's my code, and it is a bit lengthy as I manually implement a custom stack of queen using linked list( but I think you only needs to take a look at the nqueens, queen, box, board files, the others are irrelevant):
Board.h
#ifndef LINKEDLIST_TEMPLATE_BOARD_H
#define LINKEDLIST_TEMPLATE_BOARD_H


#include "Box.h"
#include "Queen.h"

class Board:public sf::Drawable, sf::Transformable {
private:
    Box** boxes;
    int n;
    int rows;
    int cols;


public:
    int getRow();
    int getCol();
    void addBox(Box& box);
    //Queen newQueen();
    //void printBoard();
    Board();
    Board(int size);
    Box**createArray(int new_rows, int new_cols);
    //void fillArray();
    //Box newBox();
    void draw(sf::RenderTarget &window, sf::RenderStates state) const;
    void addQueen(Queen queen);
};


#endif //LINKEDLIST_TEMPLATE_BOARD_H
Board.cpp
#include "Board.h"
void Board::addBox(Box &box) {
    for (int i = 0; i< rows; i ++)
    {
        for (int j = 0; j < cols; j++)
        {
            boxes[i][j] = box;
        }
    }
}
void printBoard(){}
Board::Board(){}
Board::Board(int size) {
    cols = 0;
    rows = 0;
    this->createArray(size,size);
}
Box** Board::createArray(int new_rows, int new_cols)
{
    rows = new_rows;
    cols = new_cols;
    boxes = new Box*[rows];
    for (int i = 0; i < rows; i++)
    {
        boxes[i] = new Box[cols];
    }
    return boxes;
}
/*void Board::fillArray() {
    for (int i = 0; i< rows; i ++)
    {
        for (int j = 0; j < cols; j++)
        {
            boxes[i][j] = newBox();
        }
    }
}*/

void Board::draw(sf::RenderTarget &window, sf::RenderStates states) const {
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            window.draw(boxes[i][j],states);
        }
    }
}
void Board::addQueen(Queen queen) {
    boxes[queen.getRow()][queen.getCol()] = queen;
}
int Board::getRow() {
    return this->rows;
}
int Board::getCol() {
    return this->cols;
}
Box.h
#ifndef LINKEDLIST_TEMPLATE_BOX_H
#define LINKEDLIST_TEMPLATE_BOX_H


#include <SFML/Graphics.hpp>

class Box:public sf::Drawable, sf::Transformable {
protected:
    int col;
    int row;
    sf::Color color;
    sf::RectangleShape boxBackGround;
public:
   //sf::RectangleShape newBox(float x,float y);
    void draw(sf::RenderTarget &window, sf::RenderStates state) const;
    int getRow();
    int getCol();
    void setRow(int row);
    void setCol(int col);
    Box(int row, int col);
    void setBox();
    Box();
};
Box.cpp
#include "Box.h"
int Box::getCol() {
    return col;
}
int Box::getRow() {
    return row;
}
void Box::setCol(int col) {
    this->col = col;
}
void Box::setRow(int row) {
    this->row = row;
}
Box::Box(){
    this->setBox();
}
Box::Box(int row, int col) {
    this->row = row;
    this->col = col;
    this->setBox();

}
void Box::setBox() {
    boxBackGround.setSize({200.f,200.f});
    boxBackGround.setFillColor(sf::Color::White);
    boxBackGround.setPosition(210,210);
}
/*sf::RectangleShape Box::newBox(float x, float y)
{
    sf::RectangleShape s;
    s.setSize({200.f,200.f});
    s.setFillColor(sf::Color::White);
    s.setPosition(x,y);
    return s;
}*/

void Box::draw(sf::RenderTarget &window, sf::RenderStates state) const {
    window.draw(boxBackGround);
}
Queen.h
#ifndef LINKEDLIST_TEMPLATE_QUEEN_H
#define LINKEDLIST_TEMPLATE_QUEEN_H

#include "Box.h"

class Queen: public Box {
private:
    sf::Sprite queen;
    sf::Texture texture;
public:
    Queen();
    Queen(int row, int col);
    void draw(sf::RenderTarget &window, sf::RenderStates state) const;
    void setTexture();
    void setQueen();
};
Queen.cpp
#include "Queen.h"
Queen::Queen(){
    this->setTexture();
    this->setQueen();
}
Queen::Queen(int row, int col) {
    this->setCol(col);
    this->setRow(row);
    this->setTexture();
    this->setQueen();
}
void Queen::draw(sf::RenderTarget &window, sf::RenderStates state) const
{
    window.draw(this->queen);
}
void Queen::setTexture() {
    this->texture.loadFromFile("queen.png");
}
void Queen::setQueen() {
    this->queen.setTexture(this->texture);
    this->queen.setPosition(this->boxBackGround.getGlobalBounds().height-20,this->boxBackGround.getGlobalBounds().width-20);
}
NQueens.h
#ifndef LINKEDLIST_TEMPLATE_NQUEENS_H
#define LINKEDLIST_TEMPLATE_NQUEENS_H
#include"Stack.h"
#include"Queen.h"
#include "Board.h"
#include<cmath>
#include <SFML/Graphics.hpp>


class NQueens {
private:
    sf::RenderWindow*  window;
    bool isConflict();
    bool success;
    Stack<Queen> queens;
    Board board;
    Box theBox;
    int n;

public:
    NQueens(int n);
    NQueens();
    void run();
    void placeQueens();
    void initWindow();

};
NQueens.cpp
#include "NQueens.h"
#include <cmath>

NQueens::NQueens(int n): board(n),n(n){
    initWindow();
}
NQueens::NQueens(){}
void NQueens::run()
{
    placeQueens();
    board.addBox(theBox);
    int size = queens.size();
    for (int i =0;i<size;i++)
    {
        Queen temp = queens.pop();
        board.addQueen(temp);

    }
    while(this->window->isOpen())
    {
        sf::Event event;
        while(this->window->pollEvent(event)){
            if (event.type == sf::Event::Closed){
                this->window->close();
            }
        }
        this->window->clear(sf::Color::Black);
        this->window->draw(board);
        this->window->display();
    }
}
bool NQueens::isConflict() {
    if (queens.empty())
        return false;
    Queen top = queens.front();
    for (int i = 1; i <queens.size();i++)
    {
        if(queens.seek(i).getRow() == top.getRow())
            return true;
        if (queens.seek(i).getCol() == top.getCol())
            return true;
        if (abs(queens.seek(i).getCol()- top.getCol()) == abs(queens.seek(i).getRow() - top.getRow()))
            return true;
    }
    return false;
}
void NQueens::placeQueens()
{
    Queen q(1,1);
    success = false;
    queens.push(q);
    while (!success && !queens.empty())
    {

        if (isConflict())
        {
            while (!queens.empty() && queens.front().getCol() == n)
            {
                queens.pop();
            }
            if(!queens.empty())
            {
                Queen temp = queens.pop();
                temp.setCol(temp.getCol()+1);
                queens.push(temp);
            }
        }
        else if (queens.size() == n)
        {
            success = true;
        }
        else
        {
            Queen temp(queens.size()+1, 1);
            queens.push(temp);
        }
    }
}
void NQueens::initWindow() {
    this->window = new sf::RenderWindow(sf::VideoMode(1980,1080),"NQUEENS");
}
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "NQueens.h"
int main() {
    NQueens nq(5);
    nq.run();
}
Any help is much appreciated! Thank you and sorry for the lengthy post.

Pages: [1]