very cool but check out what i get as text and console output. any ideas why?
(http://i.cubeupload.com/pfoUTY.png)
#include "stdafx.h"
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include "imgui.h"
#include "imgui-SFML.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "ImGui test!");
ImGui::SFML::SetWindow(window);
ImGui::SFML::InitImGui();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
ImGui::SFML::ProcessEvent(event);
if (event.type == sf::Event::Closed) window.close();
}
ImGui::SFML::UpdateImGui();
static bool ui = true;
if (ImGui::Begin("Test!", &ui, ImVec2(10, 10)))
{
ImGui::Text("Hello, world!");
}
ImGui::End();
ImGui::Render();
window.display();
}
return 0;
}
now how can one get that string of bytes from a binary file?
I'm not sure it even works (correctly) but... uhm... you get the idea:
#include <iostream>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
int main(int argc, char** argv)
{
std::cout << "Enter the number of bytes to add a new line or use 0 for continuous." << std::endl;
std::cout << "Row length: ";
unsigned int row_len = 0;
std::cin >> row_len;
std::cout << "Enter 1 if you want spacing between array elements or 0 if you don't." << std::endl;
std::cout << "Element space: ";
bool elem_space = false;
std::cin >> elem_space;
std::cout << "Enter the path of the input file that should be converted." << std::endl;
std::cout << "File path: ";
std::string src_path;
std::cin >> src_path;
if (src_path.empty()) {
std::cout << "No path to the source file was specified. Aborting..." << std::endl;
return 1;
}
std::ifstream src_file(src_path, std::ifstream::binary);
if (!src_file.is_open()) {
std::cout << "Unable to open source file: " << src_path << std::endl;
return 1;
}
std::streampos src_size = 0;
src_size = src_file.tellg();
src_file.seekg(0, std::ios::end);
src_size = src_file.tellg() - src_size;
src_file.seekg(0, std::ios::beg);
std::string dest_path(src_path);
dest_path.append(".cpp");
std::cout << "Note: Output data will be saved into " << dest_path << std::endl;
std::ofstream dest_file(dest_path);
if (!dest_file.is_open()) {
std::cout << "Unable to open destination file: " << src_path << std::endl;
src_file.close();
return 1;
}
std::cout << "transferring from " << src_path << " to " << dest_path << \
" with a row length of " << row_len << std::endl;
std::vector<unsigned char> src_data;
src_data.reserve(src_size);
src_data.insert(src_data.begin(), \
std::istream_iterator<unsigned char>(src_file), \
std::istream_iterator<unsigned char>());
dest_file << "static const unsigned char file_data[] = {\n";
unsigned int byte_count = 0;
for (std::vector<unsigned char>::iterator itr = src_data.begin(); itr != src_data.end(); ++itr)
{
if (itr + 1 != src_data.end()) {
dest_file << "0x" << std::hex << std::setfill('0') << std::setw(2) \
<< int(*itr) << (elem_space ? ", " : ",");
} else {
dest_file << "0x" << std::hex << std::setfill('0') << std::setw(2) \
<< int(*itr) << "\n};";
break;
}
++byte_count;
if(row_len > 0 && byte_count % row_len == 0) dest_file << "\n";
}
src_file.close();
dest_file.close();
return 0;
}
Good work. Unfortunately for me the clipping problem really made it a no go in its current state...
So to solve that I replaced the ImImpl_RenderDrawLists with the one you get from ImGuis example OpenGl implementation but modified to work with SFML textures... If someone else wants to use it remember you need to link opengl32.lib(or whatever is the equivalent if you aren't using VS)...
#include <SFML/OpenGL.hpp>
[...]
static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
{
if (cmd_lists_count == 0)
return;
ImImpl_window->pushGLStates();
// We are using the OpenGL fixed pipeline to make the example code simpler to read!
// A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnable(GL_TEXTURE_2D);
// Setup orthographic projection matrix
const float width = ImGui::GetIO().DisplaySize.x;
const float height = ImGui::GetIO().DisplaySize.y;
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Render command lists
for (int n = 0; n < cmd_lists_count; n++)
{
const ImDrawList* cmd_list = cmd_lists[n];
const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->vtx_buffer.front();
glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
int vtx_offset = 0;
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
{
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
sf::Texture::bind((sf::Texture*)pcmd->texture_id);
glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
vtx_offset += pcmd->vtx_count;
}
}
// Restore modified state
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
ImImpl_window->popGLStates();
}
There is probably a way so that you don't need to call push/popGLStates but I don't know OpenGL so I can't fix that...