Browse Source

Move example code into it's own directory. Added example scene. Added scene and view hierarchy.

main
Nichole Mattera 2 years ago
parent
commit
005a34a969
11 changed files with 193 additions and 16 deletions
  1. +3
    -0
      .gitignore
  2. +2
    -2
      Makefile
  3. +8
    -2
      example/main.cpp
  4. +33
    -0
      example/scenes/ExampleScene.cpp
  5. +29
    -0
      example/scenes/ExampleScene.hpp
  6. +32
    -8
      src/Application.cpp
  7. +11
    -3
      src/Application.hpp
  8. +25
    -0
      src/Scene.cpp
  9. +1
    -1
      src/Scene.hpp
  10. +31
    -0
      src/View.cpp
  11. +18
    -0
      src/View.hpp

+ 3
- 0
.gitignore View File

@ -0,0 +1,3 @@
build
dist
.vscode

+ 2
- 2
Makefile View File

@ -32,11 +32,11 @@ include $(DEVKITPRO)/libnx/switch_rules
TARGET := $(notdir $(CURDIR))
BUILD := build
DIST := dist
SOURCES := src $(wildcard src/**)
SOURCES := example $(wildcard example/**) src $(wildcard src/**)
EXEFS_SRC := exefs_src
ROMFS := romfs
APP_TITLE := Skyline
APP_TITLE := Skyline Example
APP_AUTHOR := Steven Mattera
APP_VERSION := 1.0.0


src/main.cpp → example/main.cpp View File

@ -1,5 +1,5 @@
//
// Skyline
// Skyline Example
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
@ -18,12 +18,18 @@
//
#include <switch.h>
#include "Application.hpp"
#include "../src/Application.hpp"
#include "scenes/ExampleScene.hpp"
using namespace skyline;
using namespace skylineExample;
int main(int argc, char **argv) {
Application * app = new Application();
ExampleScene * rootScene = new ExampleScene();
app->setRootScene(rootScene);
app->start();
delete rootScene;
delete app;
}

+ 33
- 0
example/scenes/ExampleScene.cpp View File

@ -0,0 +1,33 @@
//
// Skyline Example
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#include "ExampleScene.hpp"
#include "../../src/Application.hpp"
using namespace skyline;
namespace skylineExample {
ExampleScene::~ExampleScene() {}
void ExampleScene::handleButton(u32 kDown) {
if (kDown & KEY_PLUS) {
Application::sharedApplication->popScene();
}
}
}

+ 29
- 0
example/scenes/ExampleScene.hpp View File

@ -0,0 +1,29 @@
//
// Skyline Example
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#include "../../src/Scene.hpp"
namespace skylineExample {
class ExampleScene : public skyline::Scene {
public:
~ExampleScene();
void handleButton(u32 kDown);
};
}

+ 32
- 8
src/Application.cpp View File

@ -22,39 +22,63 @@
namespace skyline {
Application::Application() {
Application::sharedApplication = this;
this->_window = nwindowGetDefault();
framebufferCreate(&this->_framebuffer, this->_window, FB_WIDTH, FB_HEIGHT, PIXEL_FORMAT_RGBA_8888, 2);
framebufferMakeLinear(&this->_framebuffer);
setsysInitialize();
setsysGetColorSetId(&Application::colorSetId);
setsysGetColorSetId(&colorSetId);
setsysExit();
this->backgroundColor = (Application::colorSetId == ColorSetId_Light) ? SLColorMake(235, 235, 235, 255) : SLColorMake(45, 45, 45, 255);
this->backgroundColor = (colorSetId == ColorSetId_Light) ? SLColorMake(235, 235, 235, 255) : SLColorMake(45, 45, 45, 255);
this->_currentScene = NULL;
}
Application::~Application() {
Application::sharedApplication = NULL;
_sceneStack.clear();
framebufferClose(&this->_framebuffer);
}
void Application::setRootScene(Scene * scene) {
this->_sceneStack.clear();
this->pushScene(scene);
}
void Application::pushScene(Scene * scene) {
this->_sceneStack.push_back(scene);
this->_currentScene = scene;
}
void Application::popScene() {
this->_sceneStack.pop_back();
if (_sceneStack.size() == 0) {
this->_currentScene = NULL;
} else {
this->_currentScene = this->_sceneStack.back();
}
}
// Public Methods
void Application::start() {
while (appletMainLoop()) {
hidScanInput();
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS)
this->_currentScene->handleButton(kDown);
if (this->_currentScene == NULL)
break;
Draw::framebuffer = (u8 *) framebufferBegin(&this->_framebuffer, &Draw::stride);
Draw::drawFilledRectangle(SLRectMake(0, 0, FB_WIDTH, FB_HEIGHT), this->backgroundColor);
if (currentScene != NULL) {
currentScene->handleButton(kDown);
currentScene->render(SLRectMake(0, 0, FB_WIDTH, FB_HEIGHT), 0);
}
// TODO: Calculate delta time.
this->_currentScene->render(SLRectMake(0, 0, FB_WIDTH, FB_HEIGHT), 0);
framebufferEnd(&this->_framebuffer);
}


+ 11
- 3
src/Application.hpp View File

@ -20,22 +20,30 @@
#pragma once
#include <switch.h>
#include <vector>
#include "Scene.hpp"
#include "Types.hpp"
namespace skyline {
class Application {
public:
static inline Application * sharedApplication;
Application();
~Application();
void start();
void setRootScene(Scene * scene);
void pushScene(Scene * scene);
void popScene();
static inline Scene * currentScene;
static inline ColorSetId colorSetId;
void start();
ColorSetId colorSetId;
Color backgroundColor;
private:
NWindow * _window;
Framebuffer _framebuffer;
std::vector<Scene *> _sceneStack;
Scene * _currentScene;
};
}

+ 25
- 0
src/Scene.cpp View File

@ -20,5 +20,30 @@
#include "Scene.hpp"
namespace skyline {
Scene::Scene() {}
Scene::~Scene() {}
void Scene::handleButton(u32 kDown) {}
void Scene::render(Rect rect, double dTime) {
for (auto const& view : this->subviews) {
if (!view->hidden) {
s16 x = rect.origin.x + view->frame.origin.x;
s16 y = rect.origin.y + view->frame.origin.y;
s16 w = view->frame.size.w;
s16 h = view->frame.size.h;
view->render(SLRectMake(x, y, w, h), dTime);
}
}
}
void Scene::addSubView(View * view) {
view->superview = NULL;
this->subviews.push_back(view);
}
void Scene::removeSubView(View * view) {
view->superview = NULL;
this->subviews.remove(view);
}
}

+ 1
- 1
src/Scene.hpp View File

@ -30,7 +30,7 @@ namespace skyline {
Scene();
virtual ~Scene();
virtual void handleButton(u32 buttons);
virtual void handleButton(u32 kDown);
virtual void render(Rect rect, double dTime);
/* View Hierarchy */


+ 31
- 0
src/View.cpp View File

@ -20,5 +20,36 @@
#include "View.hpp"
namespace skyline {
View::View() : View(SLRectMake(0, 0, 0, 0)) {}
View::View(Rect frame) {
this->frame = frame;
this->hidden = false;
}
View::~View() {
this->subviews.clear();
}
void View::render(Rect rect, double dTime) {
for (auto const& view : this->subviews) {
if (!view->hidden) {
s16 x = rect.origin.x + view->frame.origin.x;
s16 y = rect.origin.y + view->frame.origin.y;
s16 w = view->frame.size.w;
s16 h = view->frame.size.h;
view->render(SLRectMake(x, y, w, h), dTime);
}
}
}
void View::addSubView(View * view) {
view->superview = view;
this->subviews.push_back(view);
}
void View::removeSubView(View * view) {
view->superview = NULL;
this->subviews.remove(view);
}
}

+ 18
- 0
src/View.hpp View File

@ -19,8 +19,26 @@
#pragma once
#include <switch.h>
#include <list>
#include "Types.hpp"
namespace skyline {
class View {
public:
Rect frame;
bool hidden;
View();
View(Rect frame);
virtual ~View();
virtual void render(Rect rect, double dTime);
/* View Hierarchy */
View * superview;
std::list<View *> subviews;
void addSubView(View * view);
void removeSubView(View * view);
};
}

Loading…
Cancel
Save