Skip to content

Commit

Permalink
Qt frontend WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Feb 9, 2020
1 parent f914958 commit f71b475
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 11 deletions.
20 changes: 12 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
cmake_minimum_required(VERSION 2.8)
project(NanoboyAdvance C CXX)
project(NanoboyAdvance CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)

set(PLATFORM "sdl" CACHE STRING "Build Target (sdl, ...)")
option(FRONTEND_QT "Enable Qt5 frontend" ON)
option(FRONTEND_SDL "Enable SDL2 frontend" ON)

set(COMPILE_FLAGS "${COMPILE_FLAGS} -Ofast")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILE_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}")
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU OR CMAKE_CXX_COMPILER_ID STREQUAL Clang)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast")

option(PROFILE_GPROF "Profile with gprof" OFF)
if (PROFILE)
add_definitions(-pg)
set(CMAKE_EXE_LINKER_FLAGS -pg)
endif()
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()

#add_definitions(-pg)
#set(CMAKE_EXE_LINKER_FLAGS -pg)

add_subdirectory(source)
12 changes: 11 additions & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
include_directories(.)

option(PLATFORM_QT "Enable Qt5 frontend" ON)
option(PLATFORM_SDL "Enable SDL2 frontend" ON)

add_subdirectory(gba)
add_subdirectory("platform/${PLATFORM}")

if (PLATFORM_QT)
add_subdirectory("platform/qt")
endif()

if (PLATFORM_SDL)
add_subdirectory("platform/sdl")
endif()
22 changes: 22 additions & 0 deletions source/platform/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SET(SOURCES
main.cpp
mainwindow.cpp
screen.cpp
)

SET(HEADERS
mainwindow.hpp
screen.hpp
)

# TODO: do we really need this line?
set(CMAKE_AUTOMOC ON)

find_package(OpenGL REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5OpenGL REQUIRED)
set(QT_DEPS Qt5::Gui Qt5::Widgets Qt5::OpenGL)

ADD_EXECUTABLE(NanoboyAdvance ${SOURCES} ${HEADERS})
TARGET_LINK_LIBRARIES(NanoboyAdvance gba ${QT_DEPS} ${OPENGL_gl_LIBRARY})
13 changes: 13 additions & 0 deletions source/platform/qt/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <QApplication>
#include "mainwindow.hpp"

int main(int argc, char** argv) {
QApplication app{ argc, argv };
MainWindow window{ &app };

QCoreApplication::setOrganizationName("fleroviux");
QCoreApplication::setApplicationName("NanoboyAdvance");

window.show();
return app.exec();
}
79 changes: 79 additions & 0 deletions source/platform/qt/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2020 fleroviux (Frederic Meyer)
*
* This file is part of NanoboyAdvance.
*
* NanoboyAdvance 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 3 of the License, or
* (at your option) any later version.
*
* NanoboyAdvance 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 NanoboyAdvance. If not, see <http://www.gnu.org/licenses/>.
*/

// TEST
#include <gba/emulator.hpp>
#include <thread>

#include <QApplication>
#include <QMenuBar>

#include "mainwindow.hpp"

MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent) {
setWindowTitle("NanoboyAdvance");
app->installEventFilter(this);

/* Setup OpenGL widget */
screen = std::make_shared<Screen>(this);
setCentralWidget(screen.get());

/* Create menu bar */
auto menubar = new QMenuBar(this);
setMenuBar(menubar);

/* Create file menu */
auto file_menu = menubar->addMenu(tr("&File"));
auto file_open = file_menu->addAction(tr("&Open"));
auto file_close = file_menu->addAction(tr("&Close"));

/* Create help menu */
auto help_menu = menubar->addMenu(tr("&?"));

connect(file_open, &QAction::triggered, this, [this] {
//auto config = std::make_shared<GameBoyAdvance::Config>();
//config->video_dev = screen;

//auto emulator = std::make_unique<GameBoyAdvance::Emulator>(config);
//emulator->LoadGame("violet.gba");
//emulator->Reset();
//for (int i = 0; i < 20000; i++) {
// emulator->Frame();
//}

std::thread test([&] {
auto config = std::make_shared<GameBoyAdvance::Config>();
config->video_dev = screen;

auto emulator = std::make_unique<GameBoyAdvance::Emulator>(config);
emulator->LoadGame("violet.gba");
emulator->Reset();

while (true) {
emulator->Frame();
}
});

test.detach();
});
}

MainWindow::~MainWindow() {

}
36 changes: 36 additions & 0 deletions source/platform/qt/mainwindow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) 2020 fleroviux (Frederic Meyer)
*
* This file is part of NanoboyAdvance.
*
* NanoboyAdvance 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 3 of the License, or
* (at your option) any later version.
*
* NanoboyAdvance 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 NanoboyAdvance. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <memory>
#include <QMainWindow>

#include "screen.hpp"

class MainWindow : public QMainWindow {
Q_OBJECT

public:
MainWindow(QApplication* app, QWidget* parent = 0);
~MainWindow();

private:
std::shared_ptr<Screen> screen;
};
89 changes: 89 additions & 0 deletions source/platform/qt/screen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (C) 2020 fleroviux (Frederic Meyer)
*
* This file is part of NanoboyAdvance.
*
* NanoboyAdvance 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 3 of the License, or
* (at your option) any later version.
*
* NanoboyAdvance 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 NanoboyAdvance. If not, see <http://www.gnu.org/licenses/>.
*/

#include "screen.hpp"

void Screen::Draw(std::uint32_t* buffer) {
emit SignalDraw(buffer);
}

void Screen::DrawSlot(std::uint32_t* buffer) {
glBindTexture(GL_TEXTURE_2D, texture);

/* Update texture pixels */
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
240, /* TODO: do not hardcode the dimensions? */
160,
0,
GL_BGRA,
GL_UNSIGNED_BYTE,
buffer
);

/* Redraw screen */
update();
}

void Screen::initializeGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
}

void Screen::paintGL() {
glViewport(viewport_x, 0, viewport_width, viewport_height);
glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(-1.0f, 1.0f);

glTexCoord2f(1.0f, 0);
glVertex2f(1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, -1.0f);

glTexCoord2f(0, 1.0f);
glVertex2f(-1.0f, -1.0f);
}
glEnd();
}

void Screen::resizeGL(int width, int height) {
viewport_width = height + height / 2;
viewport_height = height;
viewport_x = (width - viewport_width) / 2;
}
64 changes: 64 additions & 0 deletions source/platform/qt/screen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (C) 2020 fleroviux (Frederic Meyer)
*
* This file is part of NanoboyAdvance.
*
* NanoboyAdvance 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 3 of the License, or
* (at your option) any later version.
*
* NanoboyAdvance 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 NanoboyAdvance. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <QGLWidget>
#include <QOpenGLWidget>
#include <gba/config/device/video_device.hpp>

class Screen : public QOpenGLWidget,
public GameBoyAdvance::VideoDevice {
Q_OBJECT

public:
Screen(QWidget* parent) : QOpenGLWidget(parent) {
QSurfaceFormat format;
format.setSwapInterval(0);
setFormat(format);

connect(this, &Screen::SignalDraw, this, &Screen::DrawSlot);
}

~Screen() override { glDeleteTextures(1, &texture); }

void Draw(std::uint32_t* buffer) final;

auto sizeHint() const -> QSize {
return QSize{ 480, 320 };
}

protected:
void initializeGL() override;
void paintGL() override;
void resizeGL(int width, int height) override;

private slots:
void DrawSlot(std::uint32_t* buffer);

signals:
void SignalDraw(std::uint32_t* buffer);

private:
int viewport_x = 0;
int viewport_width = 0;
int viewport_height = 0;

GLuint texture;
};
3 changes: 1 addition & 2 deletions source/platform/sdl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ FIND_PACKAGE(SDL2 REQUIRED)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR})

ADD_EXECUTABLE(NanoboyAdvance-SDL ${SOURCES} ${HEADERS})
TARGET_LINK_LIBRARIES(NanoboyAdvance-SDL gba)
TARGET_LINK_LIBRARIES(NanoboyAdvance-SDL ${SDL2_LIBRARY})
TARGET_LINK_LIBRARIES(NanoboyAdvance-SDL gba ${SDL2_LIBRARY})

0 comments on commit f71b475

Please sign in to comment.