Skip to content

Commit

Permalink
added grid system and text
Browse files Browse the repository at this point in the history
  • Loading branch information
nanda-gopal-sb committed Jan 19, 2025
1 parent 1f324a1 commit 5f8d396
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 104 deletions.
101 changes: 1 addition & 100 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,100 +1 @@
# CMake SFML Project Template

This repository template should allow for a fast and hassle-free kick start of your next SFML project using CMake.
Thanks to [GitHub's nature of templates](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template), you can fork this repository without inheriting its Git history.

The template starts out very basic, but might receive additional features over time:

- Basic CMake script to build your project and link SFML on any operating system
- Basic [GitHub Actions](https://github.com/features/actions) script for all major platforms

## How to Use

1. Install [Git](https://git-scm.com/downloads) and [CMake](https://cmake.org/download/). Use your system's package manager if available.
2. Follow [GitHub's instructions](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template) for how to use their project template feature to create your own project. If you don't want to use GitHub, see the section below.
3. Clone your new GitHub repo and open the repo in your text editor of choice.
4. Open [CMakeLists.txt](CMakeLists.txt). Rename the project and the target name of the executable to whatever name you want. Make sure to change all occurrences.
5. If you want to add or remove any .cpp files, change the source files listed in the `add_executable` call in CMakeLists.txt to match the source files your project requires. If you plan on keeping the default main.cpp file then no changes are required.
6. If your code uses the Audio or Network modules then add `sfml-audio` or `sfml-network` to the `target_link_libraries` call alongside the existing `sfml-graphics` library that is being linked.
7. If you use Linux, install SFML's dependencies using your system package manager. On Ubuntu and other Debian-based distributions you can use the following commands:
```
sudo apt update
sudo apt install \
libxrandr-dev \
libxcursor-dev \
libudev-dev \
libfreetype-dev \
libflac-dev \
libvorbis-dev \
libgl1-mesa-dev \
libegl1-mesa-dev \
libfreetype-dev
```
8. Configure and build your project. Most popular IDEs support CMake projects with very little effort on your part.

- [VS Code](https://code.visualstudio.com) via the [CMake extension](https://code.visualstudio.com/docs/cpp/cmake-linux)
- [Visual Studio](https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-170)
- [CLion](https://www.jetbrains.com/clion/features/cmake-support.html)
- [Qt Creator](https://doc.qt.io/qtcreator/creator-project-cmake.html)

Using CMake from the command line is straightforward as well.
Be sure to run these commands in the root directory of the project you just created.

```
cmake -B build
cmake --build build
```

9. Enjoy!

## Upgrading SFML

SFML is found via CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module.
FetchContent automatically downloads SFML from GitHub and builds it alongside your own code.
Beyond the convenience of not having to install SFML yourself, this ensures ABI compatibility and simplifies things like specifying static versus shared libraries.

Modifying what version of SFML you want is as easy as changing the `GIT_TAG` argument.
Currently it uses SFML 3 via the `3.0.0` tag.

## But I want to...

Modify CMake options by adding them as configuration parameters (with a `-D` flag) or by modifying the contents of CMakeCache.txt and rebuilding.

### Not use GitHub

You can use this project without a GitHub account by [downloading the contents](https://github.com/SFML/cmake-sfml-project/archive/refs/heads/master.zip) of the repository as a ZIP archive and unpacking it locally.
This approach also avoids using Git entirely if you would prefer to not do that.

### Change Compilers

See the variety of [`CMAKE_<LANG>_COMPILER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html) options.
In particular you'll want to modify `CMAKE_CXX_COMPILER` to point to the C++ compiler you wish to use.

### Change Compiler Optimizations

CMake abstracts away specific optimizer flags through the [`CMAKE_BUILD_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html) option.
By default this project recommends `Release` builds which enable optimizations.
Other build types include `Debug` builds which enable debug symbols but disable optimizations.
If you're using a multi-configuration generator (as is often the case on Windows), you can modify the [`CMAKE_CONFIGURATION_TYPES`](https://cmake.org/cmake/help/latest/variable/CMAKE_CONFIGURATION_TYPES.html#variable:CMAKE_CONFIGURATION_TYPES) option.

### Change Generators

While CMake will attempt to pick a suitable default generator, some systems offer a number of generators to choose from.
Ubuntu, for example, offers Makefiles and Ninja as two potential options.
For a list of generators, click [here](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html).
To modify the generator you're using you must reconfigure your project providing a `-G` flag with a value corresponding to the generator you want.
You can't simply modify an entry in the CMakeCache.txt file unlike the above options.
Then you may rebuild your project with this new generator.

## More Reading

Here are some useful resources if you want to learn more about CMake:

- [Official CMake Tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/)
- [How to Use CMake Without the Agonizing Pain - Part 1](https://alexreinking.com/blog/how-to-use-cmake-without-the-agonizing-pain-part-1.html)
- [How to Use CMake Without the Agonizing Pain - Part 2](https://alexreinking.com/blog/how-to-use-cmake-without-the-agonizing-pain-part-2.html)
- [Better CMake YouTube series by Jefferon Amstutz](https://www.youtube.com/playlist?list=PL8i3OhJb4FNV10aIZ8oF0AA46HgA2ed8g)

## License

The source code is dual licensed under Public Domain and MIT -- choose whichever you prefer.
## Work in progress
Binary file added assests/daFont.ttf
Binary file not shown.
90 changes: 86 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,85 @@
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <random>
#include <string>
#include <vector>
#include <iostream>

#define N 9
#define M 9
#define CELL_SIZE 80



struct Cell{
int number = 1;
int x; // obvious
int y; // obvious
Cell(int x1, int y1)
{
x = x1;
y = y1;
}
};

std::vector<Cell> cells;


sf::Text DrawTextForBoard()
{
sf::Font font("assests/daFont.ttf");
sf::Text text(font);
text.setCharacterSize(64);
text.setFillColor(sf::Color::Black);
return text;
}
void generateSudoku(){
std::random_device rd;
std::mt19937 gen(rd()); // seeding the number generator
std::uniform_int_distribution<> distrib(0, 9);
for(int i = 0 ; i<81; i++){
cells[i].number = distrib(gen);
}
}
void fillCell()
{
for (int a = 0; a < 9; a++)
{
for (int b = 0; b < 9; b++)
{
cells.push_back(Cell(b, a)); // inversed as SFML has invered axis for some reason
}
}
generateSudoku();
}

void getMousPos(sf::RenderWindow &window)
{
int mouse_x = sf::Mouse::getPosition(window).x / CELL_SIZE;
int mouse_y = sf::Mouse::getPosition(window).y / CELL_SIZE;
std::cout<<cells[mouse_x + mouse_y * 9].number;
}
void drawRectangles(sf::RenderWindow &window){
sf::Font font("assests/daFont.ttf");
sf::Text text(font);
text.setCharacterSize(64);
text.setFillColor(sf::Color::Black);
sf::RectangleShape rectangle({CELL_SIZE-3, CELL_SIZE-3});
for(int i = 0 ; i<9 ; i++){
for(int j = 0 ; j<9 ; j++){
text.setPosition({float(CELL_SIZE*i),float(CELL_SIZE*j)});
text.setString(std::to_string(cells[i+j*9].number));
rectangle.setPosition({float(CELL_SIZE*i) ,float(CELL_SIZE*j) });
window.draw(rectangle);
window.draw(text);
}
}
}


int main()
{
auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "CMake SFML Project");
fillCell();
auto window = sf::RenderWindow(sf::VideoMode({9*80u, 9*80u}), "Sudoku");
window.setFramerateLimit(144);

while (window.isOpen())
Expand All @@ -13,12 +90,17 @@ int main()
while (const std::optional event = window.pollEvent())
{
// "close requested" event: we close the window
if (event->is<sf::Event::Closed>())
if (event->is<sf::Event::Closed>()){
window.close();
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)){
getMousPos(window);
}
}
window.clear();
drawRectangles(window);
window.display();

}
}
//lmao
}

0 comments on commit 5f8d396

Please sign in to comment.