Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "RandomApp.h"
- #include "Font.h"
- #include "Input.h"
- #include <imgui_glfw3.h>
- #include <random>
- const int GALAXY_WIDTH = 100;
- const int GALAXY_HEIGHT = 100;
- RandomApp::RandomApp() {
- }
- RandomApp::~RandomApp() {
- }
- bool RandomApp::startup() {
- m_2dRenderer = new aie::Renderer2D();
- m_font = new aie::Font("./font/consolas.ttf", 32);
- m_seed = 1;
- m_galaxy = nullptr;
- aie::ImGui_Init(getWindowPtr(), true);
- return true;
- }
- void RandomApp::shutdown() {
- delete m_font;
- delete m_2dRenderer;
- }
- void RandomApp::fillGalaxy()
- {
- if (m_galaxy == nullptr) {
- m_galaxy = new char[GALAXY_WIDTH * GALAXY_HEIGHT];
- }
- memset(m_galaxy, 0, GALAXY_WIDTH * GALAXY_HEIGHT);
- srand(m_seed);
- for (int y = 0; y < GALAXY_HEIGHT; y++) {
- for (int x = 0; x < GALAXY_WIDTH; x++) {
- int probability = rand() % 100;
- if (probability > 70) {
- m_galaxy[y * GALAXY_WIDTH + x] = 0x01;
- }
- }
- }
- }
- int RandomApp::starAt(int galaxy, int nX, int nY)
- {
- int nReturn = 0;
- srand(galaxy);
- for (int y = 0; y <= nY; y++) {
- for (int x = 0; x <= nX; x++) {
- nReturn = rand() % 2;
- }
- }
- return nReturn;
- }
- int RandomApp::starAt2(int galaxy, int nX, int nY)
- {
- int nReturn = 0;
- srand(galaxy * (nX + (GALAXY_WIDTH*nY)));
- int probability = rand() % 100;
- if (probability > 70)
- return 0x01;
- return 0;
- }
- int RandomApp::starAt3(int galaxy, int nX, int nY)
- {
- int nReturn = 0;
- srand(galaxy * (nX + (GALAXY_WIDTH*nY)));
- int probability = rand() % 100;
- if (probability > 70)
- return 0x01;
- return 0;
- }
- void RandomApp::update(float deltaTime) {
- // input example
- aie::Input* input = aie::Input::getInstance();
- ImGui::InputInt("Seed", &m_seed);
- if (ImGui::Button("Big Fat Array")) {
- m_sample = Samples::technique1;
- fillGalaxy();
- }
- if (ImGui::Button("Generate As Needed")) {
- m_sample = Samples::technique2;
- }
- if (ImGui::Button("Generate As Needed 2")) {
- m_sample = Samples::technique3;
- }
- if (ImGui::Button("Generate As Needed 3")) {
- m_sample = Samples::technique4;
- }
- // exit the application
- if (input->isKeyDown(aie::INPUT_KEY_ESCAPE))
- quit();
- }
- void RandomApp::draw() {
- // wipe the screen to the background colour
- clearScreen();
- // begin drawing sprites
- m_2dRenderer->begin();
- // draw your stuff here!
- switch (m_sample) {
- case Samples::technique1:
- {
- if (m_galaxy == nullptr)
- break;
- for (int y = 0; y < GALAXY_HEIGHT; y++) {
- for (int x = 0; x < GALAXY_WIDTH; x++) {
- if (m_galaxy[y * GALAXY_WIDTH + x] != 0) {
- m_2dRenderer->drawCircle(x << 4, y << 4, 4);
- }
- }
- }
- break;
- }
- case Samples::technique2:
- {
- for (int y = 0; y < GALAXY_HEIGHT>>1; y++) {
- for (int x = 0; x < GALAXY_WIDTH>>1; x++) {
- if (starAt(m_seed, x, y) != 0) {
- m_2dRenderer->drawCircle(x << 4, y << 4, 4);
- }
- }
- }
- break;
- }
- case Samples::technique3:
- {
- for (int y = 0; y < GALAXY_HEIGHT; y++) {
- for (int x = 0; x < GALAXY_WIDTH; x++) {
- if (starAt2(m_seed, x, y) != 0) {
- m_2dRenderer->drawCircle(x << 4, y << 4, 4);
- }
- }
- }
- break;
- }
- case Samples::technique4:
- {
- for (int y = 0; y < GALAXY_HEIGHT; y++) {
- for (int x = 0; x < GALAXY_WIDTH; x++) {
- if (starAt3(m_seed, x, y) != 0) {
- m_2dRenderer->drawCircle(x << 4, y << 4, 4);
- }
- }
- }
- break;
- }
- }
- // output some text
- m_2dRenderer->drawText(m_font, "Press ESC to quit", 0, 0);
- // done drawing sprites
- m_2dRenderer->end();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement