mirror of
https://github.com/encounter/cpp3ds.git
synced 2026-03-30 11:04:22 -07:00
Added display() template for synchronize buffer swapping of all screens.
This commit is contained in:
@@ -31,8 +31,7 @@ extern "C" int main() {
|
||||
bottom_screen.draw(stage);
|
||||
top_screen.draw(stage);
|
||||
|
||||
bottom_screen.display();
|
||||
top_screen.display();
|
||||
Screen::display(top_screen, bottom_screen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef CPP3DS_DISPLAY_H
|
||||
#define CPP3DS_DISPLAY_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <cpp3ds/Screen.h>
|
||||
#include <cpp3ds/TopScreen.h>
|
||||
|
||||
namespace cpp3ds {
|
||||
|
||||
template<class... T>
|
||||
void display(Screen& screen, T&... rest){
|
||||
screen._display();
|
||||
display(rest...);
|
||||
}
|
||||
|
||||
// template<class... T>
|
||||
// void display(TopScreen& screen, T&... rest){
|
||||
// screen._display();
|
||||
// display(rest...);
|
||||
// }
|
||||
|
||||
void display();
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -8,6 +8,9 @@
|
||||
|
||||
namespace cpp3ds {
|
||||
|
||||
/**
|
||||
* Handles the main game loop for you.
|
||||
*/
|
||||
class Scene {
|
||||
protected:
|
||||
uint64_t lastTime;
|
||||
@@ -15,9 +18,10 @@ namespace cpp3ds {
|
||||
BottomScreen bottomScreen;
|
||||
TopScreen topScreen;
|
||||
Input input;
|
||||
void display();
|
||||
virtual int update(float deltaTime) = 0;
|
||||
virtual void render() = 0;
|
||||
int run();
|
||||
int run(bool useDisplay = true);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <cpp3ds/Screen.h>
|
||||
|
||||
#define BOTTOM_X (400-320)/2
|
||||
#define BOTTOM_Y 240
|
||||
#define BOTTOM_Y 240 + SIM_OUTLINE_THICKNESS*2
|
||||
#define BOTTOM_WIDTH 320
|
||||
#define BOTTOM_HEIGHT 240
|
||||
|
||||
|
||||
+18
-14
@@ -1,22 +1,26 @@
|
||||
#ifndef INPUT_H
|
||||
#define INPUT_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
namespace cpp3ds {
|
||||
|
||||
enum Button {
|
||||
BUTTON_A = 1,
|
||||
BUTTON_B = 2,
|
||||
BUTTON_X = 1024,
|
||||
BUTTON_Y = 2048,
|
||||
BUTTON_UP = 64,
|
||||
BUTTON_DOWN = 128,
|
||||
BUTTON_LEFT = 32,
|
||||
BUTTON_RIGHT = 16,
|
||||
BUTTON_L1 = 512,
|
||||
BUTTON_R1 = 256,
|
||||
BUTTON_START = 8,
|
||||
BUTTON_SELECT = 4
|
||||
};
|
||||
typedef sf::Keyboard::Key Button;
|
||||
|
||||
// enum Button {
|
||||
// BUTTON_A = 1,
|
||||
// BUTTON_B = 2,
|
||||
// BUTTON_X = 1024,
|
||||
// BUTTON_Y = 2048,
|
||||
const Button BUTTON_UP = sf::Keyboard::Up;
|
||||
const Button BUTTON_DOWN = sf::Keyboard::Down;
|
||||
const Button BUTTON_LEFT = sf::Keyboard::Left;
|
||||
const Button BUTTON_RIGHT = sf::Keyboard::Right;
|
||||
// BUTTON_L1 = 512,
|
||||
// BUTTON_R1 = 256,
|
||||
// BUTTON_START = 8,
|
||||
// BUTTON_SELECT = 4
|
||||
// };
|
||||
|
||||
enum Event {
|
||||
EVENT_DOWN,
|
||||
|
||||
@@ -23,7 +23,13 @@ namespace cpp3ds {
|
||||
int getHeight(){ return height; }
|
||||
void setPixel(int x, int y, Color color);
|
||||
void clear(Color color = {0,0,0});
|
||||
void display();
|
||||
|
||||
virtual void _display();
|
||||
|
||||
template<class... T>
|
||||
friend void display(Screen& screen, T&... rest);
|
||||
friend void display();
|
||||
|
||||
virtual void draw(Drawable& obj, float x = 0, float y = 0, bool use3D = false);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#define TOP_LEFT_Y 0
|
||||
#define TOP_LEFT_FRAME1 0x20184E60
|
||||
#define TOP_LEFT_FRAME2 0x201CB370
|
||||
#define TOP_RIGHT_X 400
|
||||
#define TOP_RIGHT_X 400 + SIM_OUTLINE_THICKNESS*2
|
||||
#define TOP_RIGHT_Y 0
|
||||
#define TOP_RIGHT_FRAME1 0x20282160
|
||||
#define TOP_RIGHT_FRAME2 0x202C8670
|
||||
@@ -29,7 +29,7 @@ namespace cpp3ds {
|
||||
right_screen(TOP_RIGHT_X, TOP_RIGHT_Y, TOP_WIDTH, TOP_HEIGHT)
|
||||
{};
|
||||
void clear(Color color = {0,0,0});
|
||||
void display();
|
||||
void _display();
|
||||
virtual void draw(Drawable& obj, float x = 0, float y = 0, bool use3D = true);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <sim3ds/sim/Simulator.h>
|
||||
#include <sim3ds/sim/SFMLWidget.h>
|
||||
|
||||
#define SIM_OUTLINE_THICKNESS 1
|
||||
|
||||
namespace cpp3ds {
|
||||
|
||||
class Simulator{
|
||||
@@ -21,7 +23,6 @@ namespace cpp3ds {
|
||||
Gtk::Scale *scale3D;
|
||||
|
||||
sf::Thread* thread;
|
||||
sf::Mutex mutex;
|
||||
|
||||
sf::Texture pausedFrameTexture;
|
||||
sf::Sprite pausedFrame;
|
||||
@@ -40,8 +41,9 @@ namespace cpp3ds {
|
||||
|
||||
public:
|
||||
SFMLWidget *screen;
|
||||
sf::Mutex mutex;
|
||||
|
||||
bool triggerStop = false;
|
||||
bool triggerPause = false;
|
||||
bool isPaused = true;
|
||||
bool isThreadRunning = false;
|
||||
|
||||
|
||||
+21
-21
@@ -102,7 +102,6 @@
|
||||
</object>
|
||||
<object class="GtkWindow" id="window">
|
||||
<property name="can_focus">False</property>
|
||||
<property name="resizable">False</property>
|
||||
<child>
|
||||
<object class="GtkBox" id="mainBox">
|
||||
<property name="visible">True</property>
|
||||
@@ -265,6 +264,24 @@
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkStatusbar" id="statusbar">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="margin_left">10</property>
|
||||
<property name="margin_right">10</property>
|
||||
<property name="margin_top">6</property>
|
||||
<property name="margin_bottom">6</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<property name="spacing">2</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">False</property>
|
||||
<property name="pack_type">end</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkToolbar" id="toolbar">
|
||||
<property name="visible">True</property>
|
||||
@@ -304,7 +321,7 @@
|
||||
<object class="GtkScale" id="scale3D">
|
||||
<property name="visible">True</property>
|
||||
<property name="sensitive">False</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="opacity">0.80000000000000004</property>
|
||||
<property name="adjustment">value3D</property>
|
||||
<property name="fill_level">100</property>
|
||||
@@ -361,7 +378,7 @@
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
@@ -377,29 +394,12 @@
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">2</property>
|
||||
<property name="position">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<placeholder/>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkStatusbar" id="statusbar">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="margin_left">10</property>
|
||||
<property name="margin_right">10</property>
|
||||
<property name="margin_top">6</property>
|
||||
<property name="margin_bottom">6</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<property name="spacing">2</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">4</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <sim3ds/sim/Simulator.h>
|
||||
|
||||
namespace cpp3ds {
|
||||
|
||||
void display(){
|
||||
_simulator->screen->display();
|
||||
_simulator->screen->renderWindow.clear();
|
||||
while (_simulator->isPaused)
|
||||
sf:sleep(sf::milliseconds(100));
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <cpp3ds/Input.h>
|
||||
#include <cpp3ds/utils.h>
|
||||
|
||||
@@ -13,7 +14,7 @@ namespace cpp3ds {
|
||||
|
||||
bool Input::isDown(Button button) {
|
||||
// return (~read_word(HID) & button);
|
||||
return false;
|
||||
return sf::Keyboard::isKeyPressed(button);
|
||||
}
|
||||
|
||||
void Input::update3DSlider(){
|
||||
|
||||
+2
-4
@@ -67,7 +67,7 @@ void SFMLWidget::on_realize(){
|
||||
//Set initial position and size of the Gdk::Window:
|
||||
attributes.x = allocation.get_x();
|
||||
attributes.y = allocation.get_y();
|
||||
attributes.width = allocation.get_width()*2;
|
||||
attributes.width = 800;
|
||||
attributes.height = allocation.get_height();
|
||||
|
||||
attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
|
||||
@@ -81,10 +81,8 @@ void SFMLWidget::on_realize(){
|
||||
|
||||
// transparent background
|
||||
this->unset_background_color();
|
||||
|
||||
this->set_double_buffered(false);
|
||||
|
||||
this->set_redraw_on_allocate(false);
|
||||
this->set_can_focus(true);
|
||||
|
||||
//make the widget receive expose events
|
||||
m_refGdkWindow->set_user_data(gobj());
|
||||
|
||||
+18
-10
@@ -1,20 +1,28 @@
|
||||
#include <stdint.h>
|
||||
#include <cpp3ds/Screen.h>
|
||||
#include <cpp3ds/Scene.h>
|
||||
#include <cpp3ds/Display.h>
|
||||
#include <cpp3ds/utils.h>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
namespace cpp3ds {
|
||||
|
||||
int Scene::run(){
|
||||
int ret = 99;
|
||||
// float deltaTime;
|
||||
// lastTime = GetSystemTick();
|
||||
// while (ret == 0) {
|
||||
// render();
|
||||
// deltaTime = (float)(GetSystemTick() - lastTime) / TICKS_PER_SEC;
|
||||
// lastTime = GetSystemTick();
|
||||
// ret = update(deltaTime);
|
||||
// }
|
||||
int Scene::run(bool useDisplay){
|
||||
int ret = 0;
|
||||
sf::Clock clock;
|
||||
sf::Time deltaTime;
|
||||
while (ret == 0) {
|
||||
render();
|
||||
if (useDisplay)
|
||||
display();
|
||||
deltaTime = clock.restart();
|
||||
ret = update(deltaTime.asSeconds());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Scene::display(){
|
||||
cpp3ds::display(bottomScreen, topScreen);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+9
-8
@@ -1,4 +1,3 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <sim3ds/sim/Simulator.h>
|
||||
#include <cpp3ds/Screen.h>
|
||||
#include <cpp3ds/Color.h>
|
||||
@@ -16,8 +15,12 @@ namespace cpp3ds {
|
||||
void Screen::clear(Color color){
|
||||
sf::RectangleShape box(sf::Vector2f(width, height));
|
||||
sf::Color c(color.r, color.g, color.b);
|
||||
sf::Color outline(255,255,255, 10);
|
||||
box.setFillColor(c);
|
||||
box.setOutlineColor(outline);
|
||||
box.setOutlineThickness(SIM_OUTLINE_THICKNESS);
|
||||
box.setPosition(x,y);
|
||||
pixelImage.create(width, height, sf::Color::Transparent);
|
||||
// Should only clear the relevant screen area
|
||||
_simulator->screen->renderWindow.draw(box);
|
||||
}
|
||||
@@ -27,14 +30,12 @@ namespace cpp3ds {
|
||||
obj.draw(*this, x, y, use3D, true);
|
||||
}
|
||||
|
||||
void Screen::display() {
|
||||
void Screen::_display() {
|
||||
sf::Texture starsTexture;
|
||||
starsTexture.loadFromImage(pixelImage);
|
||||
sf::Sprite test(starsTexture);
|
||||
test.setPosition(x, y);
|
||||
|
||||
_simulator->screen->renderWindow.draw(test);
|
||||
_simulator->screen->display();
|
||||
starsTexture.loadFromImage(pixelImage);
|
||||
sf::Sprite test(starsTexture);
|
||||
test.setPosition(x, y);
|
||||
_simulator->screen->renderWindow.draw(test);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+8
-6
@@ -20,7 +20,7 @@ namespace cpp3ds {
|
||||
// aboutDialog->set_logo(logo);
|
||||
|
||||
// TODO: Make pausedFrame the cpp3ds logo?
|
||||
pausedFrameTexture.create(800,480);
|
||||
pausedFrameTexture.create(800 + SIM_OUTLINE_THICKNESS*2, 480 + SIM_OUTLINE_THICKNESS*2);
|
||||
pausedFrame.setTexture(pausedFrameTexture);
|
||||
|
||||
builder->get_widget("saveDialog", saveDialog);
|
||||
@@ -33,10 +33,12 @@ namespace cpp3ds {
|
||||
builder->get_widget("scale3D", scale3D);
|
||||
|
||||
// Create and add a SFMLWidget
|
||||
screen = new SFMLWidget(sf::VideoMode(800, 480));
|
||||
screen = new SFMLWidget(sf::VideoMode(800 + SIM_OUTLINE_THICKNESS*2, 480 + SIM_OUTLINE_THICKNESS*2));
|
||||
boxSFML->pack_start(*screen, true, true);
|
||||
screen->show();
|
||||
|
||||
screen->renderWindow.setFramerateLimit(60);
|
||||
|
||||
Glib::signal_timeout().connect(sigc::bind_return(sigc::mem_fun(*this,
|
||||
&Simulator::checkThreadState ),true),200);
|
||||
|
||||
@@ -82,14 +84,14 @@ namespace cpp3ds {
|
||||
}
|
||||
|
||||
void Simulator::play() {
|
||||
triggerPause = false;
|
||||
isPaused = false;
|
||||
screen->renderWindow.setActive(false);
|
||||
if (!isThreadRunning)
|
||||
thread->launch();
|
||||
}
|
||||
|
||||
void Simulator::pause() {
|
||||
triggerPause = true;
|
||||
isPaused = true;
|
||||
}
|
||||
|
||||
void Simulator::stop() {
|
||||
@@ -130,7 +132,7 @@ namespace cpp3ds {
|
||||
void Simulator::on_sfml_size_allocate(Gtk::Allocation& allocation){
|
||||
// Dirty hack to trigger event AFTER resize
|
||||
Glib::signal_timeout().connect_once(sigc::mem_fun(*this,
|
||||
&Simulator::drawPausedFrame ),10);
|
||||
&Simulator::drawPausedFrame ),50);
|
||||
}
|
||||
|
||||
void Simulator::on_playpause_clicked(){
|
||||
@@ -157,7 +159,7 @@ namespace cpp3ds {
|
||||
scale3D->set_sensitive(active);
|
||||
scale3D->set_opacity(active ? 1.0 : 0.8);
|
||||
screen->show3D();
|
||||
screen->set_size_request(active ? 800 : 400, 480);
|
||||
screen->set_size_request(active ? 800 + SIM_OUTLINE_THICKNESS*2 : 400, 480 + SIM_OUTLINE_THICKNESS*2);
|
||||
// Resize window to smallest size allowed
|
||||
window->resize(1,1);
|
||||
}
|
||||
|
||||
+3
-3
@@ -21,9 +21,9 @@ namespace cpp3ds {
|
||||
}
|
||||
}
|
||||
|
||||
void TopScreen::display(){
|
||||
left_screen.display();
|
||||
right_screen.display();
|
||||
void TopScreen::_display(){
|
||||
left_screen._display();
|
||||
right_screen._display();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+8
-1
@@ -1,15 +1,18 @@
|
||||
#include <stdint.h>
|
||||
#include <cpp3ds/Screen.h>
|
||||
#include <cpp3ds/Scene.h>
|
||||
#include <cpp3ds/utils.h>
|
||||
|
||||
namespace cpp3ds {
|
||||
|
||||
int Scene::run(){
|
||||
int Scene::run(bool useDisplay){
|
||||
int ret = 0;
|
||||
float deltaTime;
|
||||
lastTime = GetSystemTick();
|
||||
while (ret == 0) {
|
||||
render();
|
||||
if (useDisplay)
|
||||
display();
|
||||
deltaTime = (float)(GetSystemTick() - lastTime) / TICKS_PER_SEC;
|
||||
lastTime = GetSystemTick();
|
||||
ret = update(deltaTime);
|
||||
@@ -17,4 +20,8 @@ namespace cpp3ds {
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Scene::display(){
|
||||
Screen::display(bottomScreen, topScreen);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user