From 0c01a02baf8453fc2b1c676f54b3f1e945d93147 Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Fri, 27 May 2016 14:28:58 -0400 Subject: [PATCH 01/49] Update FBI sendfile script for 2.x protocol --- scripts/sendfile.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/sendfile.py b/scripts/sendfile.py index f4adc59..23f5e10 100644 --- a/scripts/sendfile.py +++ b/scripts/sendfile.py @@ -3,16 +3,19 @@ def sendfile(filename, ip): statinfo = os.stat(filename) - fbiinfo = struct.pack('!q', statinfo.st_size) with open(filename, 'rb') as f: sock = socket.socket() sock.connect((ip, 5000)) - sock.send(fbiinfo) - while True: - chunk = f.read(16384) - if not chunk: - break # EOF - sock.sendall(chunk) + sock.send(struct.pack('!i', 1)) + if struct.unpack("!b", sock.recv(1))[0] == 1: + sock.send(struct.pack('!q', statinfo.st_size)) + while True: + chunk = f.read(1024 * 256) + if not chunk: + break # EOF + sock.sendall(chunk) + else: + print("Canceled by FBI") sock.close() def show_usage_exit(): From e87d589154441c9bf35d880b6c5a9e4a46760f52 Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Sat, 28 May 2016 12:06:05 -0400 Subject: [PATCH 02/49] Add system font --- include/cpp3ds/Graphics/RenderTarget.hpp | 2 + include/cpp3ds/Graphics/Text.hpp | 12 ++ src/cpp3ds/Graphics/Console.cpp | 2 + src/cpp3ds/Graphics/Text.cpp | 155 ++++++++++++++++++++++- 4 files changed, 167 insertions(+), 4 deletions(-) diff --git a/include/cpp3ds/Graphics/RenderTarget.hpp b/include/cpp3ds/Graphics/RenderTarget.hpp index f82ee5c..5070dda 100644 --- a/include/cpp3ds/Graphics/RenderTarget.hpp +++ b/include/cpp3ds/Graphics/RenderTarget.hpp @@ -49,6 +49,8 @@ class Drawable; //////////////////////////////////////////////////////////// class RenderTarget : NonCopyable { +friend class Text; + public : //////////////////////////////////////////////////////////// diff --git a/include/cpp3ds/Graphics/Text.hpp b/include/cpp3ds/Graphics/Text.hpp index a3d8d89..65f06e4 100644 --- a/include/cpp3ds/Graphics/Text.hpp +++ b/include/cpp3ds/Graphics/Text.hpp @@ -40,6 +40,7 @@ namespace cpp3ds { + //////////////////////////////////////////////////////////// /// \brief Graphical text that can be drawn to a render target /// @@ -124,6 +125,8 @@ namespace cpp3ds //////////////////////////////////////////////////////////// void setFont(const Font& font); + void useSystemFont(); + //////////////////////////////////////////////////////////// /// \brief Set the character size /// @@ -337,6 +340,8 @@ namespace cpp3ds //////////////////////////////////////////////////////////// virtual void draw(RenderTarget& target, RenderStates states) const; + void drawSystemFont(RenderTarget& target, RenderStates states) const; + //////////////////////////////////////////////////////////// /// \brief Make sure the text's geometry is updated /// @@ -346,6 +351,9 @@ namespace cpp3ds //////////////////////////////////////////////////////////// void ensureGeometryUpdate() const; + void ensureGeometryUpdateSystemFont() const; + Vector2f findCharacterPosSystemFont(std::size_t index) const; + //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// @@ -360,6 +368,10 @@ namespace cpp3ds mutable VertexArray m_outlineVertices; ///< Vertex array containing the outline geometry mutable FloatRect m_bounds; ///< Bounding rectangle of the text (in local coordinates) mutable bool m_geometryNeedUpdate; ///< Does the geometry need to be recomputed? + bool m_useSystemFont; ///< Flag to use 3DS system font +#ifndef EMULATION + mutable std::vector m_systemGlyphTextures; +#endif }; } // namespace cpp3ds diff --git a/src/cpp3ds/Graphics/Console.cpp b/src/cpp3ds/Graphics/Console.cpp index e70a373..fab52dd 100644 --- a/src/cpp3ds/Graphics/Console.cpp +++ b/src/cpp3ds/Graphics/Console.cpp @@ -87,6 +87,7 @@ void Console::enable(Screen screen, Color color) console.m_memoryText.setFont(console.m_font); console.m_memoryText.setCharacterSize(12); + console.m_memoryText.useSystemFont(); console.m_screen = screen; console.m_limit = 1000; @@ -159,6 +160,7 @@ void Console::write(String text) { Text line(text, m_font, 10); line.setFillColor(m_color); + line.useSystemFont(); m_lines.push_back(line); } diff --git a/src/cpp3ds/Graphics/Text.cpp b/src/cpp3ds/Graphics/Text.cpp index ef4597a..0125dd2 100644 --- a/src/cpp3ds/Graphics/Text.cpp +++ b/src/cpp3ds/Graphics/Text.cpp @@ -30,6 +30,11 @@ #include #include #include +#include +#ifndef EMULATION +#include "CitroHelpers.hpp" +#include +#endif namespace @@ -73,6 +78,11 @@ void addGlyphQuad(cpp3ds::VertexArray& vertices, cpp3ds::Vector2f position, cons namespace cpp3ds { + +#ifndef EMULATION +extern cpp3ds::Texture *system_font_textures; +#endif + namespace priv { // Default font for Text objects for user convenience @@ -92,7 +102,8 @@ Text::Text() : m_vertices (Triangles), m_outlineVertices (Triangles), m_bounds (), - m_geometryNeedUpdate(false) + m_geometryNeedUpdate(false), + m_useSystemFont (false) { } @@ -110,7 +121,8 @@ Text::Text(const String& string, const Font& font, unsigned int characterSize) : m_vertices (Triangles), m_outlineVertices (Triangles), m_bounds (), - m_geometryNeedUpdate(true) + m_geometryNeedUpdate(true), + m_useSystemFont (false) { } @@ -134,10 +146,21 @@ void Text::setFont(const Font& font) { m_font = &font; m_geometryNeedUpdate = true; + m_useSystemFont = false; } } +//////////////////////////////////////////////////////////// +void Text::useSystemFont() +{ +#ifndef EMULATION + m_geometryNeedUpdate = true; + m_useSystemFont = true; +#endif +} + + //////////////////////////////////////////////////////////// void Text::setCharacterSize(unsigned int size) { @@ -256,9 +279,19 @@ float Text::getOutlineThickness() const } +//////////////////////////////////////////////////////////// +Vector2f Text::findCharacterPosSystemFont(std::size_t index) const +{ + return Vector2f(); +} + + //////////////////////////////////////////////////////////// Vector2f Text::findCharacterPos(std::size_t index) const { + if (m_useSystemFont) + return findCharacterPosSystemFont(index); + // Make sure that we have a valid font if (!m_font) return Vector2f(); @@ -318,10 +351,58 @@ FloatRect Text::getGlobalBounds() const } +//////////////////////////////////////////////////////////// +void Text::drawSystemFont(RenderTarget& target, RenderStates states) const +{ + ensureGeometryUpdate(); + states.transform *= getTransform(); +#ifndef EMULATION + if (target.m_cache.viewChanged) + target.applyCurrentView(); + if (states.blendMode != target.m_cache.lastBlendMode) + target.applyBlendMode(states.blendMode); + if (states.shader) + target.applyShader(states.shader); + + target.applyTransform(states.transform); + Mtx_Identity(MtxStack_Cur(CitroGetTextureMatrix())); + CitroUpdateMatrixStacks(); + + C3D_BufInfo* bufInfo = C3D_GetBufInfo(); + BufInfo_Init(bufInfo); + BufInfo_Add(bufInfo, &m_vertices[0], sizeof(Vertex), 3, 0x210); + + C3D_TexEnv* env = C3D_GetTexEnv(0); + C3D_TexEnvSrc(env, C3D_RGB, GPU_PRIMARY_COLOR, 0, 0); + C3D_TexEnvSrc(env, C3D_Alpha, GPU_TEXTURE0, GPU_PRIMARY_COLOR, 0); + C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); + C3D_TexEnvFunc(env, C3D_RGB, GPU_REPLACE); + C3D_TexEnvFunc(env, C3D_Alpha, GPU_MODULATE); + + int vertexIndex = 0; + int lastTextureIndex = -1; + for (Uint16 textureIndex : m_systemGlyphTextures) + { + if (lastTextureIndex != textureIndex) { + lastTextureIndex = textureIndex; + C3D_TexBind(0, system_font_textures[textureIndex].getNativeTexture()); + } + C3D_DrawArrays(GPU_TRIANGLE_STRIP, vertexIndex, 4); + vertexIndex += 4; + } + Texture::bind(NULL); +#endif +} + + //////////////////////////////////////////////////////////// void Text::draw(RenderTarget& target, RenderStates states) const { - if (m_font) + if (m_useSystemFont) + { + drawSystemFont(target, states); + } + else if (m_font) { ensureGeometryUpdate(); @@ -337,11 +418,71 @@ void Text::draw(RenderTarget& target, RenderStates states) const } +//////////////////////////////////////////////////////////// +void Text::ensureGeometryUpdateSystemFont() const +{ +#ifndef EMULATION + m_systemGlyphTextures.clear(); + + float maxX = 0.f; + float x = 0.f; + float y = 0.f; + float scaleX = static_cast(m_characterSize) / 25.f; + float scaleY = scaleX; + bool baseline = false; + + ssize_t units; + uint32_t code; + + auto str = m_string.toUtf8(); + const uint8_t* p = str.c_str(); + float firstX = x; + int lastSheet = -1; + int vertexIndex = 0; + do + { + if (!*p) break; + units = decode_utf8(&code, p); + if (units == -1) + break; + p += units; + if (code == '\n') + { + x = firstX; + y += scaleY*fontGetInfo()->lineFeed; + } + else if (code > 0) + { + int glyphIdx = fontGlyphIndexFromCodePoint(code); + fontGlyphPos_s data; + fontCalcGlyphPos(&data, glyphIdx, GLYPH_POS_CALC_VTXCOORD, scaleX, scaleY); + + m_systemGlyphTextures.push_back(data.sheetIndex); + + m_vertices.append(Vertex(Vector2f(x+data.vtxcoord.left, y+data.vtxcoord.bottom), m_fillColor, Vector2f(data.texcoord.left, data.texcoord.bottom))); + m_vertices.append(Vertex(Vector2f(x+data.vtxcoord.right, y+data.vtxcoord.bottom), m_fillColor, Vector2f(data.texcoord.right, data.texcoord.bottom))); + m_vertices.append(Vertex(Vector2f(x+data.vtxcoord.left, y+data.vtxcoord.top), m_fillColor, Vector2f(data.texcoord.left, data.texcoord.top))); + m_vertices.append(Vertex(Vector2f(x+data.vtxcoord.right, y+data.vtxcoord.top), m_fillColor, Vector2f(data.texcoord.right, data.texcoord.top))); + + x += data.xAdvance; + if (x > maxX) + maxX = x; + } + } while (code > 0); + + m_bounds.left = 0; + m_bounds.top = 0; + m_bounds.width = maxX; + m_bounds.height = y + scaleY * fontGetInfo()->lineFeed; +#endif +} + + //////////////////////////////////////////////////////////// void Text::ensureGeometryUpdate() const { // Load system's opensans.tff if user attempts drawing without font - if (m_font == &priv::system_font && !priv::system_font_loaded) + if (!m_useSystemFont && m_font == &priv::system_font && !priv::system_font_loaded) { priv::system_font_loaded = true; priv::ResourceInfo font = priv::core_resources["opensans.ttf"]; @@ -364,6 +505,12 @@ void Text::ensureGeometryUpdate() const if (!m_font || m_string.isEmpty()) return; + if (m_useSystemFont) + { + ensureGeometryUpdateSystemFont(); + return; + } + // Compute values related to the text style bool bold = (m_style & Bold) != 0; bool underlined = (m_style & Underlined) != 0; From e46d31acc701d824ef1b5a0f1b95fd0b5a842e23 Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Sat, 28 May 2016 18:30:32 -0400 Subject: [PATCH 03/49] Fix some HTTP errors Don't close context until class is destroyed. Increase field buffer length to 1024 (enough?) --- include/cpp3ds/Network/Http.hpp | 2 ++ src/cpp3ds/Network/Http.cpp | 29 ++++++++++++++++++++++++----- src/emu3ds/Network/Http.cpp | 7 +++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/cpp3ds/Network/Http.hpp b/include/cpp3ds/Network/Http.hpp index d64e1bf..b703683 100644 --- a/include/cpp3ds/Network/Http.hpp +++ b/include/cpp3ds/Network/Http.hpp @@ -362,6 +362,8 @@ class Http : NonCopyable //////////////////////////////////////////////////////////// Http(); + ~Http(); + //////////////////////////////////////////////////////////// /// \brief Construct the HTTP client with the target host /// diff --git a/src/cpp3ds/Network/Http.cpp b/src/cpp3ds/Network/Http.cpp index f2c06de..758dd2a 100644 --- a/src/cpp3ds/Network/Http.cpp +++ b/src/cpp3ds/Network/Http.cpp @@ -32,6 +32,7 @@ #include #include #include +#include <3ds/services/httpc.h> namespace @@ -130,9 +131,9 @@ const std::string& Http::Response::getField(const std::string& field) const { return it->second; } - else + else if (m_context && m_context->httphandle) { - char val[255]; + char val[1024]; httpcGetResponseHeader(m_context, field.c_str(), val, sizeof(val)); m_fields[field] = val; return m_fields[field]; @@ -192,7 +193,7 @@ Http::Http() : m_host(), m_port(0) { - + m_context.httphandle = 0; } @@ -200,6 +201,15 @@ m_port(0) Http::Http(const std::string& host, unsigned short port) { setHost(host, port); + m_context.httphandle = 0; +} + + +//////////////////////////////////////////////////////////// +Http::~Http() +{ + if (m_context.httphandle) + httpcCloseContext(&m_context); } @@ -241,8 +251,19 @@ void Http::setHost(const std::string& host, unsigned short port) //////////////////////////////////////////////////////////// Http::Response Http::sendRequest(const Http::Request& request, Time timeout, RequestCallback callback) { + if (m_context.httphandle) + { + // TODO: check for failure + httpcCloseContext(&m_context); + m_context.httphandle = 0; + } + // First make sure that the request is valid -- add missing mandatory fields Request toSend(request); + if (!toSend.hasField("User-Agent")) + { + toSend.setField("User-Agent", "libcpp3ds-network/2.x"); + } if (!toSend.hasField("Content-Length")) { std::ostringstream out; @@ -322,8 +343,6 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout, Req } } - httpcCloseContext(&m_context); - received.m_body = receivedStr; return received; } diff --git a/src/emu3ds/Network/Http.cpp b/src/emu3ds/Network/Http.cpp index 5734921..32dbed3 100644 --- a/src/emu3ds/Network/Http.cpp +++ b/src/emu3ds/Network/Http.cpp @@ -312,6 +312,13 @@ Http::Http(const std::string& host, unsigned short port) } +//////////////////////////////////////////////////////////// +Http::~Http() +{ + // +} + + //////////////////////////////////////////////////////////// void Http::setHost(const std::string& host, unsigned short port) { From 5383cac7a8daaa26fdc26a4a670791103e28af4f Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Mon, 30 May 2016 01:41:31 -0400 Subject: [PATCH 04/49] Fix sound bug and remove unnecessary code --- include/cpp3ds/System/I18n.hpp | 4 +--- src/cpp3ds/Audio/Sound.cpp | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/include/cpp3ds/System/I18n.hpp b/include/cpp3ds/System/I18n.hpp index 4fb1e4d..767bd55 100644 --- a/include/cpp3ds/System/I18n.hpp +++ b/include/cpp3ds/System/I18n.hpp @@ -23,9 +23,7 @@ namespace { std::unique_ptr buf( new char[ size ] ); snprintf( buf.get(), size, format.c_str(), args ... ); std::string stringUtf8( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside - std::wstring stringUtf32; - cpp3ds::Utf8::toUtf32(stringUtf8.begin(), stringUtf8.end(), std::back_inserter(stringUtf32)); - return cpp3ds::String(stringUtf32); + return cpp3ds::String::fromUtf8(stringUtf8.begin(), stringUtf8.end()); } } diff --git a/src/cpp3ds/Audio/Sound.cpp b/src/cpp3ds/Audio/Sound.cpp index b639920..caf40f4 100644 --- a/src/cpp3ds/Audio/Sound.cpp +++ b/src/cpp3ds/Audio/Sound.cpp @@ -144,7 +144,7 @@ void Sound::setBuffer(const SoundBuffer& buffer) memset(&m_ndspWaveBuf, 0, sizeof(ndspWaveBuf)); m_ndspWaveBuf.data_vaddr = buffer.getSamples(); - m_ndspWaveBuf.nsamples = buffer.getSampleCount(); + m_ndspWaveBuf.nsamples = buffer.getSampleCount() / buffer.getChannelCount(); m_ndspWaveBuf.looping = m_loop; // Loop enabled m_ndspWaveBuf.status = NDSP_WBUF_FREE; @@ -184,7 +184,7 @@ void Sound::setPlayingOffset(Time timeOffset) m_playOffset = timeOffset; int offset = m_buffer->getSampleRate() * m_buffer->getChannelCount() * timeOffset.asSeconds(); m_ndspWaveBuf.data_vaddr = m_buffer->getSamples() + offset; - m_ndspWaveBuf.nsamples = m_buffer->getSampleCount() - offset; + m_ndspWaveBuf.nsamples = m_buffer->getSampleCount() / m_buffer->getChannelCount() - offset; if (status == Playing) ndspChnWaveBufAdd(m_channel, &m_ndspWaveBuf); } From dbb9b597c8809bf43178b468d1d53c1758b8c1bc Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Mon, 30 May 2016 03:58:56 -0400 Subject: [PATCH 05/49] Add version to CIA build and update README --- README.md | 6 ++++-- cmake/cpp3ds.cmake | 14 +++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ee3383e..c114b1c 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,14 @@ Requirements - DevkitARM - ctrulib -- [gl3ds](https://github.com/cpp3ds/gl3ds) +- citro3d For emulator: -- [SFML 2.1](http://www.sfml-dev.org/index.php) +- [SFML 2.3](http://www.sfml-dev.org/index.php) - [Qt 5](https://qt-project.org/) +- OpenAL +- libvorbis For unit tests: diff --git a/cmake/cpp3ds.cmake b/cmake/cpp3ds.cmake index cea2c38..360e494 100644 --- a/cmake/cpp3ds.cmake +++ b/cmake/cpp3ds.cmake @@ -302,16 +302,8 @@ function(__add_ncch_banner target IMAGE SOUND) endfunction() -function(add_cia_target target RSF IMAGE SOUND ) +function(add_cia_target target RSF IMAGE SOUND) get_filename_component(target_we ${target} NAME_WE) - if(${ARGC} GREATER 6) - set(APP_TITLE ${ARGV4}) - set(APP_DESCRIPTION ${ARGV5}) - set(APP_AUTHOR ${ARGV6}) - endif() - if(${ARGC} EQUAL 8) - set(APP_ICON ${ARGV7}) - endif() if(NOT APP_TITLE) set(APP_TITLE ${target}) endif() @@ -321,6 +313,9 @@ function(add_cia_target target RSF IMAGE SOUND ) if(NOT APP_AUTHOR) set(APP_AUTHOR "Unspecified Author") endif() + if(NOT APP_VERSION) + set(APP_VERSION 0) + endif() if(NOT APP_ICON) if(EXISTS ${target}.png) set(APP_ICON ${target}.png) @@ -343,6 +338,7 @@ function(add_cia_target target RSF IMAGE SOUND ) -o ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.cia -elf $ -rsf ${RSF} + -ver ${APP_VERSION} -banner ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.bnr -icon ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.smdh -DAPP_TITLE=${APP_TITLE} From f44db4f01ce90e196be6de06d2b1a2e5a71f6128 Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Mon, 30 May 2016 21:35:17 -0400 Subject: [PATCH 06/49] Allow for custom http buffer size --- include/cpp3ds/Network/Http.hpp | 2 +- src/cpp3ds/Network/Http.cpp | 7 ++++--- src/emu3ds/Network/Http.cpp | 8 +++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/cpp3ds/Network/Http.hpp b/include/cpp3ds/Network/Http.hpp index b703683..fb6ce0a 100644 --- a/include/cpp3ds/Network/Http.hpp +++ b/include/cpp3ds/Network/Http.hpp @@ -415,7 +415,7 @@ class Http : NonCopyable /// \return Server's response /// //////////////////////////////////////////////////////////// - Response sendRequest(const Request& request, Time timeout = Time::Zero, RequestCallback callback = nullptr); + Response sendRequest(const Request& request, Time timeout = Time::Zero, RequestCallback callback = nullptr, size_t bufferSize = 4096); private: diff --git a/src/cpp3ds/Network/Http.cpp b/src/cpp3ds/Network/Http.cpp index 758dd2a..b0b1858 100644 --- a/src/cpp3ds/Network/Http.cpp +++ b/src/cpp3ds/Network/Http.cpp @@ -249,7 +249,7 @@ void Http::setHost(const std::string& host, unsigned short port) //////////////////////////////////////////////////////////// -Http::Response Http::sendRequest(const Http::Request& request, Time timeout, RequestCallback callback) +Http::Response Http::sendRequest(const Http::Request& request, Time timeout, RequestCallback callback, size_t bufferSize) { if (m_context.httphandle) { @@ -314,7 +314,7 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout, Req u32 size = 0; u32 lastProcessed = 0; u32 processed = 0; - u8 buffer[4*1024]; + u8 *buffer = new u8[bufferSize]; char *charBuf = reinterpret_cast(buffer); @@ -323,7 +323,7 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout, Req while (dlret == HTTPC_RESULTCODE_DOWNLOADPENDING) { - dlret = httpcReceiveData(&m_context, buffer, sizeof(buffer)); + dlret = httpcReceiveData(&m_context, buffer, bufferSize); if (R_FAILED(ret = httpcGetDownloadSizeState(&m_context, &processed, NULL))) break; @@ -343,6 +343,7 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout, Req } } + delete[] buffer; received.m_body = receivedStr; return received; } diff --git a/src/emu3ds/Network/Http.cpp b/src/emu3ds/Network/Http.cpp index 32dbed3..dc65953 100644 --- a/src/emu3ds/Network/Http.cpp +++ b/src/emu3ds/Network/Http.cpp @@ -354,7 +354,7 @@ void Http::setHost(const std::string& host, unsigned short port) //////////////////////////////////////////////////////////// -Http::Response Http::sendRequest(const Http::Request& request, Time timeout, RequestCallback callback) +Http::Response Http::sendRequest(const Http::Request& request, Time timeout, RequestCallback callback, size_t bufferSize) { // First make sure that the request is valid -- add missing mandatory fields Request toSend(request); @@ -403,8 +403,8 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout, Req std::string receivedStr; std::size_t size = 0; std::size_t processed = 0; - char buffer[4*1024]; - while (m_connection.receive(buffer, sizeof(buffer), size) == Socket::Done) + char *buffer = new char[bufferSize]; + while (m_connection.receive(buffer, bufferSize, size) == Socket::Done) { if (callback) { @@ -433,6 +433,8 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout, Req receivedStr.append(buffer, buffer + size); } + delete[] buffer; + // Build the Response object from the received data if (!callback) received.parse(receivedStr); From 2167fa7917ad1574f64bb307e8c3d2fce4f9bfc3 Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Wed, 1 Jun 2016 12:49:06 -0400 Subject: [PATCH 07/49] Add option to use pre-built banner --- cmake/cpp3ds.cmake | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmake/cpp3ds.cmake b/cmake/cpp3ds.cmake index 360e494..b8b9f9c 100644 --- a/cmake/cpp3ds.cmake +++ b/cmake/cpp3ds.cmake @@ -330,7 +330,10 @@ function(add_cia_target target RSF IMAGE SOUND) if( NOT ${target_we}.smdh) __add_smdh(${target_we}.smdh ${APP_TITLE} ${APP_DESCRIPTION} ${APP_AUTHOR} ${APP_ICON}) endif() - __add_ncch_banner(${target_we}.bnr ${IMAGE} ${SOUND}) + if(NOT BANNER) + set(BANNER ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.bnr) + __add_ncch_banner(${target_we}.bnr ${IMAGE} ${SOUND}) + endif() add_custom_command(OUTPUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.cia COMMAND ${MAKEROM} -f cia -target t @@ -339,7 +342,7 @@ function(add_cia_target target RSF IMAGE SOUND) -elf $ -rsf ${RSF} -ver ${APP_VERSION} - -banner ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.bnr + -banner ${BANNER} -icon ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.smdh -DAPP_TITLE=${APP_TITLE} -DAPP_PRODUCT_CODE=${APP_PRODUCT_CODE} From 5c0cb16b60f4beb1e389a4798694db0377de01c2 Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Mon, 6 Jun 2016 00:50:52 -0400 Subject: [PATCH 08/49] Add HTTP::close() method for better httpc control --- include/cpp3ds/Network/Http.hpp | 2 ++ src/cpp3ds/Network/Http.cpp | 17 +++++++++++------ src/emu3ds/Network/Http.cpp | 7 +++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/cpp3ds/Network/Http.hpp b/include/cpp3ds/Network/Http.hpp index fb6ce0a..075134c 100644 --- a/include/cpp3ds/Network/Http.hpp +++ b/include/cpp3ds/Network/Http.hpp @@ -364,6 +364,8 @@ class Http : NonCopyable ~Http(); + void close(); + //////////////////////////////////////////////////////////// /// \brief Construct the HTTP client with the target host /// diff --git a/src/cpp3ds/Network/Http.cpp b/src/cpp3ds/Network/Http.cpp index b0b1858..9e61660 100644 --- a/src/cpp3ds/Network/Http.cpp +++ b/src/cpp3ds/Network/Http.cpp @@ -207,9 +207,19 @@ Http::Http(const std::string& host, unsigned short port) //////////////////////////////////////////////////////////// Http::~Http() +{ + close(); +} + + +//////////////////////////////////////////////////////////// +void Http::close() { if (m_context.httphandle) + { httpcCloseContext(&m_context); + m_context.httphandle = 0; + } } @@ -251,12 +261,7 @@ void Http::setHost(const std::string& host, unsigned short port) //////////////////////////////////////////////////////////// Http::Response Http::sendRequest(const Http::Request& request, Time timeout, RequestCallback callback, size_t bufferSize) { - if (m_context.httphandle) - { - // TODO: check for failure - httpcCloseContext(&m_context); - m_context.httphandle = 0; - } + close(); // First make sure that the request is valid -- add missing mandatory fields Request toSend(request); diff --git a/src/emu3ds/Network/Http.cpp b/src/emu3ds/Network/Http.cpp index dc65953..d29be41 100644 --- a/src/emu3ds/Network/Http.cpp +++ b/src/emu3ds/Network/Http.cpp @@ -319,6 +319,13 @@ Http::~Http() } +//////////////////////////////////////////////////////////// +void Http::close() +{ + // +} + + //////////////////////////////////////////////////////////// void Http::setHost(const std::string& host, unsigned short port) { From 2ef849024ff0713f9a2e034a40d7f7dc7943c740 Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Mon, 6 Jun 2016 01:30:21 -0400 Subject: [PATCH 09/49] Fix banner dependency in cmake --- cmake/cpp3ds.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/cpp3ds.cmake b/cmake/cpp3ds.cmake index b8b9f9c..53fe60e 100644 --- a/cmake/cpp3ds.cmake +++ b/cmake/cpp3ds.cmake @@ -347,7 +347,7 @@ function(add_cia_target target RSF IMAGE SOUND) -DAPP_TITLE=${APP_TITLE} -DAPP_PRODUCT_CODE=${APP_PRODUCT_CODE} -DAPP_UNIQUE_ID=${APP_UNIQUE_ID} - DEPENDS ${target} ${RSF} ${ROMFS_FILES} ${SHADER_OUTPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.bnr ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.smdh + DEPENDS ${target} ${RSF} ${ROMFS_FILES} ${SHADER_OUTPUT} ${BANNER} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target_we}.smdh WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} VERBATIM ) From 53eec38cc88542c2432db8af983e3657ae0247b4 Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Fri, 10 Jun 2016 15:45:35 -0400 Subject: [PATCH 10/49] Get unit test framework working --- .build.sh | 2 + CMakeLists.txt | 10 ++- Dockerfile | 10 ++- include/cpp3ds/Emulator.hpp | 11 ++- src/cpp3ds/System/FileSystem.cpp | 12 ++- src/emu3ds/Window/EventManager.cpp | 6 ++ src/emu3ds/Window/Game.cpp | 9 +- src/emu3ds/Window/Keyboard.cpp | 2 + src/emu3ds/Window/Window.cpp | 8 ++ test/CMakeLists.txt | 133 +++++++++++++++++++++++++++++ test/main.cpp | 6 -- test/run.sh | 7 -- 12 files changed, 193 insertions(+), 23 deletions(-) create mode 100644 test/CMakeLists.txt delete mode 100755 test/run.sh diff --git a/.build.sh b/.build.sh index 2ccebf7..7ba379a 100644 --- a/.build.sh +++ b/.build.sh @@ -1,6 +1,7 @@ #!/bin/sh set -ex +# Copy all files needed in cpp3ds archive mkdir -p $CPP3DS/bin/ cp $DEVKITARM/bin/makerom $CPP3DS/bin/ cp $DEVKITARM/bin/3dsxtool $CPP3DS/bin/ @@ -8,4 +9,5 @@ cp $DEVKITARM/bin/bannertool $CPP3DS/bin/ cp $DEVKITARM/bin/nihstro-assemble $CPP3DS/bin/ cp -r $PORTLIBS/lib/ $CPP3DS cp -r $PORTLIBS/include/ $CPP3DS + tar -cJvf $1 cpp3ds diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dc090c..a9f2d65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ include(cpp3ds) option(BUILD_EMULATOR "Build cpp3ds emulator (Qt5 required)" ON) option(BUILD_EXAMPLES "Build all cpp3ds example projects" ON) option(BUILD_DOCS "Build doxygen documentation" OFF) +option(BUILD_TESTS "Build unit tests" OFF) option(ENABLE_OGG "Include OGG encoder/decoder classes" ON) option(ENABLE_FLAC "Include FLAC encoder/decoder classes" OFF) option(ENABLE_MP3 "Include MP3 decoder class" OFF) @@ -72,14 +73,17 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin") # compile flags set(ARCH "-march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft") set(CPP3DS_ARM_FLAGS "-g -O2 ${ARCH} -ffunction-sections -fdata-sections") -set(CPP3DS_TEST_FLAGS "-g -O2") +set(CPP3DS_TEST_FLAGS "-g -O2 -coverage") set(CPP3DS_EMU_FLAGS "-g -O2") +add_subdirectory(src) + if(BUILD_EXAMPLES) add_subdirectory(examples) endif() if(BUILD_DOCS) add_subdirectory(doc) endif() - -add_subdirectory(src) +if(BUILD_TESTS) + add_subdirectory(test) +endif() diff --git a/Dockerfile b/Dockerfile index 92b460c..968ae3b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,7 @@ COPY . /usr/src/cpp3ds WORKDIR /usr/src RUN apt-get update && apt-get -y install \ + libgtest-dev \ libsfml-dev \ libglew-dev \ qt5-default \ @@ -29,12 +30,19 @@ RUN wget -q https://github.com/cpp3ds/3ds-tools/releases/download/r4/3ds-tools-l cp 3ds-tools/* $DEVKITARM/bin && \ rm tools.tar.gz +WORKDIR /usr/src/gtest +RUN cmake . && \ + make -j4 && \ + cp *.a /usr/lib && \ + make clean + WORKDIR /usr/src/cpp3ds RUN mkdir build && \ cd build && \ - cmake -DBUILD_EMULATOR=ON -DENABLE_OGG=ON -DBUILD_EXAMPLES=OFF .. && \ + cmake -DBUILD_EMULATOR=ON -DENABLE_OGG=ON -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=ON .. && \ make -j4 && \ cd .. && \ + ./bin/tests && \ mkdir $CPP3DS && \ cp -r build/lib $CPP3DS && \ cp -r include $CPP3DS && \ diff --git a/include/cpp3ds/Emulator.hpp b/include/cpp3ds/Emulator.hpp index ff77e73..3e5955c 100644 --- a/include/cpp3ds/Emulator.hpp +++ b/include/cpp3ds/Emulator.hpp @@ -1,9 +1,16 @@ #ifndef CPP3DS_EMULATOR_HPP #define CPP3DS_EMULATOR_HPP -#include +#ifdef TEST -#include + #include + +#else + + #include + #include + +#endif #endif diff --git a/src/cpp3ds/System/FileSystem.cpp b/src/cpp3ds/System/FileSystem.cpp index b12c00d..018b2d6 100644 --- a/src/cpp3ds/System/FileSystem.cpp +++ b/src/cpp3ds/System/FileSystem.cpp @@ -6,17 +6,23 @@ namespace cpp3ds { const std::string FileSystem::getFilePath(const std::string& filename) { +#ifdef TEST + std::string pathPrefix = "../res/test/"; +#elif defined(EMULATION) + std::string pathPrefix = "../res/"; +#endif + #ifdef EMULATION std::string newpath; - if (filename.find("../res/romfs/") == 0) { + if (filename.find(pathPrefix + "romfs/") == 0) { return filename; } if (filename.find("sdmc:/") == 0) { newpath = filename; newpath.erase(0, 5); - return "../res/sdmc" + newpath; + return pathPrefix + "sdmc" + newpath; } - newpath = "../res/romfs/" + filename; + newpath = pathPrefix + "romfs/" + filename; return newpath; #else return filename; diff --git a/src/emu3ds/Window/EventManager.cpp b/src/emu3ds/Window/EventManager.cpp index 8422e59..e4cf5ab 100644 --- a/src/emu3ds/Window/EventManager.cpp +++ b/src/emu3ds/Window/EventManager.cpp @@ -8,6 +8,10 @@ namespace cpp3ds { // TODO: configurable key-mapping std::map keyMap = { + {sf::Keyboard::Up, cpp3ds::Keyboard::Up}, + {sf::Keyboard::Left, cpp3ds::Keyboard::Left}, + {sf::Keyboard::Right, cpp3ds::Keyboard::Right}, + {sf::Keyboard::Down, cpp3ds::Keyboard::Down}, {sf::Keyboard::A, cpp3ds::Keyboard::A}, {sf::Keyboard::B, cpp3ds::Keyboard::B}, {sf::Keyboard::X, cpp3ds::Keyboard::X}, @@ -52,6 +56,7 @@ bool EventManager::filterEvent(const Event& event) { //////////////////////////////////////////////////////////// void EventManager::processEvents() { +#ifndef TEST int BOTTOM_X = 40, BOTTOM_Y = 240, BOTTOM_WIDTH = 320; @@ -111,6 +116,7 @@ void EventManager::processEvents() { break; } } +#endif } //////////////////////////////////////////////////////////// diff --git a/src/emu3ds/Window/Game.cpp b/src/emu3ds/Window/Game.cpp index 744ab8d..eb164e1 100644 --- a/src/emu3ds/Window/Game.cpp +++ b/src/emu3ds/Window/Game.cpp @@ -5,7 +5,7 @@ #include #include #include -#include