void SpikeGuideRenderer::clear() { m_spikeVertices.clear(); m_pointVertices.clear(); m_spikeArray = VertexArray(); m_pointArray = VertexArray(); m_valid = true; }
void segment(Vec2 A, Vec2 B) { //globb if (glob_vert.empty())glob_vert.push_back(VertexArray(Lines)); glob_vert[0].append(A); glob_vert[0].append(B); }
gpu::VertexArray RenderingContextGL::create_vertex_array() { GLuint gl_vao = 0; glGenVertexArrays(1, &gl_vao); CHECK_GL_ERROR return VertexArray(gl_vao); }
void vsDisplayList::DrawPoint( const vsVector3D &pos ) { int index = 0; VertexArray(&pos, 1); PointsArray( &index, 1 ); ClearVertexArray(); }
Water::Water() { width = 0; length = 0; vertexArray = VertexArray(VA_INDICES); shader = Shader::DefaultWater(); tex0 = shader->GetUniform("tex0"); ticks = shader->GetUniform("time"); };
gax:: gax(): ispushed(false), bpk(Vec2(50,50),Vec2(200,100)) { cfg->SET(spiral_step); cfg->SET(spiral_start); bpk.add_bar(varname(spiral_step),&spiral_step); bpk.add_bar(varname(spiral_start),&spiral_start); bpk.add_bar(varname(how_many),&how_many); funs["gal"]=([this]() { lines.clear(); float inner, arms, startmult, stepmult, spiral_resolution; cfg->SET(spiral_resolution); cfg->SET(arms); cfg->SET(inner); cfg->SET(startmult); cfg->SET(stepmult); auto & o = circs["o"], & i = circs["i"]; float rado = wcenter.y*.9, radi = wcenter.y*inner; confpoint(o,rado,Color::Transparent,wcenter,1); confpoint(i,radi,Color::Transparent,wcenter,1); int many = how_many*arms; for(int i = 0; i < many; ++i) { float shift_angle = float(i)*PI*2/float(many); // cons_print(shift_angle); lines["arch"+strfy(i)]=VertexArray(LinesStrip); auto & thisline = lines["arch"+strfy(i)]; for (int i=0; i< spiral_resolution; i++) { float angle = 2*PI*i/float(spiral_resolution); thisline.append(Vertex(Vec2( wcenter.x+(spiral_start*startmult+stepmult*spiral_step*angle) *cos(angle+shift_angle), wcenter.y+(spiral_start*startmult+stepmult*spiral_step*angle) *sin(angle+shift_angle) ))); } } lb2(lines.size()); }); funs["gal"](); }
void SceneGPUData::Init(const Scene *scene) { mScene = scene; // Load mesh to memory for(const TriangleMesh &mesh : scene->mTriMeshes) { mVertexArrays.push_back(VertexArray(GL_STATIC_DRAW)); VertexArray &VA = mVertexArrays.back(); VA.Init(&mesh); } LOG(INFO) << "[SceneGPUData::Init] Loaded meshes to GPU."; // Load textures to memory for(const Material &mat : scene->mMaterials) { for(const Material::TextureInfo &texInfo : mat.mTextureMaps) { if(mNameToTex.find(texInfo.name) == mNameToTex.end()) { mNameToTex[texInfo.name] = Texture2D(texInfo.name.c_str(), true); } } } LOG(INFO) << "[SceneGPUData::Init] Loaded textures to GPU."; }
void vsDisplayList::LineTo( const vsVector3D &pos ) { vsVector3D v[2]; vsColor c[2]; int i[2]; v[0] = m_cursorPos; v[1] = pos; if ( m_colorSet ) { m_fifo->WriteUint8( OpCode_SetColor ); m_fifo->WriteColor( c_white ); c[0] = m_cursorColor; c[1] = m_nextLineColor; } i[0] = 0; i[1] = 1; VertexArray(v,2); if ( m_colorSet ) { ColorArray(c,2); } LineListArray(i,2); if ( m_colorSet ) { ClearColorArray(); } ClearVertexArray(); m_cursorPos = pos; m_cursorColor = m_nextLineColor; }
int main(int argc, char*argv[]) { // ████████ INITS 1 ████████ #ifndef COMMON_INITS1 cfg.init("bedlab.cfg"); ui2::init_ui(); Vec2i windowsize; Vec2i screen_resolution = { int(VideoMode::getDesktopMode().width), int(VideoMode::getDesktopMode().height) }; if (cfg.getvar<int>("auto_winsize")) { auto window_scale = cfg.getvar<Vec2>("window_scale"); windowsize = Vec2i(scal(Vec2(screen_resolution), window_scale)); } else { windowsize = cfg.getvar<Vec2i>("windowsize"); } winsize = Vec2(windowsize); //UI.init_console(); // ALWAYS AFTER SO IT CAN GET WINDOW SIZE ui2::init_console(); // ALWAYS AFTER SO IT CAN GET WINDOW SIZE wincenter = 0.5f*Vec2(windowsize); Vec2i windowpos; VideoMode::getDesktopMode().height; if (cfg.getvar<int>("stick_left")) { windowpos = Vec2i( screen_resolution.x - windowsize.x - 10, screen_resolution.y - windowsize.y - 40 ); } else windowpos = (Vec2i(5, 25)); sf::RenderWindow window(sf::VideoMode(windowsize.x, windowsize.y), "bedlab!", 7 //,sf::ContextSettings(0, 0, 1) ); window.setFramerateLimit(cfg.getvar<int>("fps_max")); frame_duration = 1.0f / cfg.getvar<int>("fps_max"); window.setPosition(windowpos); vector<string> keys; auto choice = cfg.getstr("app"); // show_keys(cfg.getstr("app"), keys); #endif // ████████ INITS2 ████████ #ifndef COMMON_INITS2 // we don't have a class/interface/struct with data, everything is local to this function, like a classic stack that all programs are anyway. // Texture cursor_tx; // if (!cursor_tx.loadFromFile(cfg.getstr("cursor"))) // cout << "did not load cursor" << endl; // Sprite cursor; // cursor.setTexture(cursor_tx); // cursor.setOrigin(3, 3); CircleShape cursor = mkcircle({ 0,0 }, Color::Transparent, 3, 1); Color background = cfg.getvar<Color>("background"); window.setMouseCursorVisible(false); Vec2 mpos; bool leftclicked = false, rightclicked = false; // view and zoom View view, ui_view; ui_view = view = window.getDefaultView(); float zoomlevel = 1; Vec2 mpos_abs; float frame_duration = 1.0f / cfg.getvar<int>("fps_max"); #endif // COMMON_INITS2 // ████████ APP ACTUAL ████████ float smaller_size = min(windowsize.y, windowsize.x); string descriptor; Transform transf; transf.translate(10, 10); transf.scale(Vec2(smaller_size, smaller_size)); transf_glob = transf; auto addvt_col = [&](Vec2 v, Color col) { glob_vert_single.append(Vertex(transf.transformPoint(v), col)); }; auto va_to_va_col = [&](mesh2d&idxd_v, Color col) { for (unsigned int i = 0; i < idxd_v.size(); ++i) { // if (i<) addvt_col(idxd_v[i].first, col); addvt_col(idxd_v[i].second, col); //addpt(idxd_v[i].first, Orange, 5); //addpt(idxd_v[i].second, Cyan, 5); } for (auto&a : idxd_v.verts) { addpt_col(a, col, 2); } }; auto stripify = [&](vector<Vec2> strip, Color col) { glob_vert_single = VertexArray(LineStrip); for (auto&a : strip) { glob_vert_single.append(Vertex(transf.transformPoint(a), col)); addpt_col(a, col, 2); } }; size_t edit_mode = 1; // ████████████████████████████████████████ sf::Sound sound; auto make_segment = [](int size, int amplitude) { vector<Int16> sample; for (int i = 0; i < size; ++i) { sample.push_back(amplitude*sin(float(i)*PI*2.0f)); } return sample; }; /* <Jonny> duration will be sample count * sample rate <Jonny> so at 44khz you'll have 44k samples per second <Jonny> if you have 88k samples that will last 2 second */ plot_bare pl; float sample_rate = 22050; auto make_tone = [&](float duration, float frequency, int amplitude_exp) { float sample_quantity = sample_rate * duration; vector<Int16> sample; int amplitude = 1 << amplitude_exp; float increment = frequency / sample_rate; float x = 0; for (int i = 0; i < sample_quantity; ++i){ sample.push_back(amplitude*sin(float(x)*PI*2.0f)); x += increment; } return sample; }; // http://sol.gfxile.net/interpolation/ auto make_tone_progressive_2 = [&](float duration, float frequency, int amplitude_exp) { float sample_quantity = sample_rate * duration; vector<Int16> sample; int amplitude = 1 << amplitude_exp; float increment = frequency / sample_rate; float x = 0; for (int i = 0; i < sample_quantity; ++i) { float soft = float(i) / sample_quantity; //soft = 1 - (1 - 2 * soft)*(1 - 2 * soft)*(1 - 2 * soft)*(1 - 2 * soft); soft = 1 - (1 - 2 * soft)*(1 - 2 * soft); soft *= amplitude; sample.push_back(soft*sin(float(x)*PI*2.0f)); x += increment; } return sample; }; auto make_tone_progressive_4 = [&](float duration, float frequency, int amplitude_exp) { float sample_quantity = sample_rate * duration; vector<Int16> sample; int amplitude = 1 << amplitude_exp; float increment = frequency / sample_rate; float x = 0; for (int i = 0; i < sample_quantity; ++i) { float soft = float(i) / sample_quantity; soft = 1 - (1 - 2 * soft)*(1 - 2 * soft)*(1 - 2 * soft)*(1 - 2 * soft); soft *= amplitude; sample.push_back(soft*sin(float(x)*PI*2.0f)); x += increment; } return sample; }; auto make_tone_progressive_6 = [&](float duration, float frequency, int amplitude_exp) { float sample_quantity = sample_rate * duration; vector<Int16> sample; int amplitude = 1 << amplitude_exp; float increment = frequency / sample_rate; float x = 0; for (int i = 0; i < sample_quantity; ++i) { float soft = float(i) / sample_quantity; soft = 1 - (1 - 2 * soft)*(1 - 2 * soft)*(1 - 2 * soft)*(1 - 2 * soft)*(1 - 2 * soft)*(1 - 2 * soft); soft *= amplitude; sample.push_back(soft*sin(float(x)*PI*2.0f)); x += increment; } return sample; }; auto sample_string2 = cfg.getstr("sound1"); auto spl1 = splitdelim(sample_string2, ','); auto params2 = splitdelim(spl1[0]); //float freq = parse<float>(params[1]); //float duration = parse<float>(params[2]); //float amplitude_exp = parse<float>(params[3]); float freq, duration, amplitude_exp, pause; int times, smoothstep_exp; dip_bars dbars(FONT, FONTSIZE, {400,20}); //dbars.add("sampling", &sampling,5, 10000); dbars.add("sample_rate", &sample_rate, 1000, 45000); dbars.add("freq", &freq,50,5000); dbars.add("duration", &duration, 0.5, 5); dbars.add("amplitude_exp", &litude_exp, 1, 14); SoundBuffer buffer; // always lived! auto load_sample2 = [&](){ auto delaystr = splitdelim(spl1[1]); vector<float> segment_sizes; float total = 0; for (auto&a : delaystr) { total += parse<float>(a); segment_sizes.push_back(parse<float>(a)); } int i = 0; vector<Int16> sample; vector<vector<Int16>> samples; for (auto&a : segment_sizes) a /= total; //for (auto&a : segment_sizes) msg(a*total); for (auto&a : segment_sizes){ auto scaled_back = duration * freq * a; int amplitude = i % 2 ? 0 : 1 << int(amplitude_exp); msg(scaled_back); samples.push_back(make_segment(scaled_back, amplitude)); ++i; } for (auto&a : samples){ concatenate(sample, a); } SoundBuffer buffer; // \param sampleRate Sample rate (number of samples to play per second) buffer.loadFromSamples(&sample[0], sample.size(), 1, sample.size()/duration); { // plut josting just plotting vector<Vec2> plot_this; int i = 0; for (auto&a : sample) { //plot_this.push_back({ float(i), 200 * float(a) / (1 << 12) }); plot_this.push_back({ float(i), float(a) }); i++; } pl.clear(); pl.from_data_normalized(plot_this); } return buffer; }; auto load_sample = [&]() { auto delaystr = splitdelim(spl1[1]); vector<float> segment_sizes; float total = 0; for (auto&a : delaystr) { total += parse<float>(a); segment_sizes.push_back(parse<float>(a)); } int i = 0; vector<Int16> sample; vector<vector<Int16>> samples; for (auto&a : segment_sizes) a /= total; //for (auto&a : segment_sizes) msg(a*total); if (smoothstep_exp == 2) { for (auto&a : segment_sizes) { samples.push_back(make_tone_progressive_2(duration*a, freq, i % 2 ? 0 : int(amplitude_exp))); ++i; } } else if (smoothstep_exp == 4){ for (auto&a : segment_sizes) { samples.push_back(make_tone_progressive_4(duration*a, freq, i % 2 ? 0 : int(amplitude_exp))); ++i; } } for (auto&a : samples) { concatenate(sample, a); } SoundBuffer buffer; // \param sampleRate Sample rate (number of samples to play per second) //sample = make_tone(2, 400, 8); //buffer.loadFromSamples(&sample[0], sample.size(), 1, sample.size() / duration); buffer.loadFromSamples(&sample[0], sample.size(), 1, sample.size() / duration); if (true) { // plut josting just plotting vector<Vec2> plot_this; int i = 0; for (auto&a : sample) { //plot_this.push_back({ float(i), 200 * float(a) / (1 << 12) }); plot_this.push_back({ float(i), float(a) }); i++; } pl.clear(); pl.from_data_normalized(plot_this); } return buffer; }; auto load_from_cfg = [ &freq, &duration, &litude_exp, &pause, ×, &descriptor, &smoothstep_exp]() { auto temp_cfg = configfile(); temp_cfg.init("bedlab.cfg"); auto sample_string = temp_cfg.getstr("sound2"); auto things = split2(sample_string, ","); auto params = split2(things[0], " "); auto segments = split2(things[1], " "); string segments_dashed = things[1]; if (segments_dashed[0] == ' ') segments_dashed = segments_dashed.substr(1); if (things[1][0] == ' ') things[1] = things[1].substr(1); if (things[2][0] == ' ') things[2] = things[2].substr(1); if (things[3][0] == ' ') things[3] = things[3].substr(1); if (things[4][0] == ' ') things[4] = things[4].substr(1); for (auto&c : segments_dashed) { if (c == ' ') c = '-'; } freq = parse<float>(params[0]); duration = parse<float>(params[1]); amplitude_exp = parse<float>(params[2]); pause = parse<float>(things[2]); times = parse<int>(things[3]); smoothstep_exp = parse<int>(things[4]); msgs(smoothstep_exp); descriptor = params[0] // freq + "_" + params[1] // duration + "_" + params[2] // amplitude_exp + "_" + segments_dashed + "_" + things[2] // pause + "_" + things[3] // times + "_" + things[4] // smoothstep_exp ; msgs(descriptor); return segments; }; auto segments = load_from_cfg(); auto flexible_expanse = [&]() { vector<float> segment_sizes, segment_concat; for (auto&a : segments) { segment_sizes.push_back(parse<float>(a)); } segment_sizes.push_back(pause); for (int i = 0; i < times; ++i) { concatenate(segment_concat, segment_sizes); } float total = 0; for (auto&a : segment_concat) total+=a; for (auto&a : segment_concat) a /= total; vector<vector<Int16>> samples; int i = 0; if (smoothstep_exp == 2) { for (auto&a : segment_concat) { samples.push_back(make_tone_progressive_2(duration*a, freq, i % 2 ? 0 : int(amplitude_exp))); ++i; } } else if (smoothstep_exp == 4) { for (auto&a : segment_concat) { samples.push_back(make_tone_progressive_4(duration*a, freq, i % 2 ? 0 : int(amplitude_exp))); ++i; } } else if (smoothstep_exp == 6) { for (auto&a : segment_concat) { samples.push_back(make_tone_progressive_6(duration*a, freq, i % 2 ? 0 : int(amplitude_exp))); ++i; } } vector<Int16> sample; for (auto&a : samples) { concatenate(sample, a); } SoundBuffer buffer; //buffer.loadFromSamples(&sample[0], sample.size(), 1, sample.size() / duration); buffer.loadFromSamples(&sample[0], sample.size(), 1, sample_rate); if (true) { // plut josting just plotting vector<Vec2> plot_this; int i = 0; for (auto&a : sample) { //plot_this.push_back({ float(i), 200 * float(a) / (1 << 12) }); plot_this.push_back({ float(i), float(a) }); i++; } pl.clear(); pl.from_data_normalized(plot_this); } return buffer; }; //buffer = load_sample(sample_string); auto make_sound = [&]() { //buffer = load_sample(); segments = load_from_cfg(); buffer = flexible_expanse(); sound.setBuffer(buffer); string filename = "rngtn_" + descriptor+".wav"; //buffer.saveToFile("file.wav"); buffer.saveToFile(filename); sound.play(); }; make_sound(); dbars.read_from_pointers(); // ████████ callbacks ████████ #ifndef LOOP_LAMBDAS draw = [&]() { window.setView(view); //////////////// OBJECTS THAT CAN ZOOMED //////////////// window.draw(glob_vert_single); for (auto&a : glob_pts)window.draw(a); for (auto&a : glob_rects)window.draw(a); for (auto&a : glob_vert)window.draw(a); for (auto&a : glob_texts)window.draw(a); // UI draw, AFTER ui view and BEFORE other draw window.setView(ui_view); //////////////// OBJECTS THAT CANNOT ZOOMED, MEANING UI //////////////// pl.draw(window); dbars.draw(window); //br.drawwithtext(window); UI.draw(window); window.draw(cursor); }; update = [&]() { }; treatkeyevent = [&](Keyboard::Key k) { switch (k) { case Keyboard::E: break; case Keyboard::I: break; case Keyboard::Q: break; case Keyboard::BackSpace: glob_pts.clear(); glob_texts.clear(); glob_rects.clear(); glob_vert.clear(); break; case Keyboard::Space: make_sound(); sound.play(); sound.setLoop(false); break; case Keyboard::S: screenshot(window); break; case Keyboard::Num1: case Keyboard::Num2: case Keyboard::Num3: case Keyboard::Num4: case Keyboard::Num5: break; } }; mousemoved = [&](Vec2 pos) { cursor.setPosition(pos); if (leftclicked); dbars.mouse_moved(pos); }; mouseclick = [&](sf::Mouse::Button button) { if (button == Mouse::Button::Left) leftclicked = true; if (button == Mouse::Button::Right) rightclicked = true; if (button == Mouse::Button::Left) dbars.mouse_click(mpos); }; mouserelease = [&](sf::Mouse::Button button) { if (button == Mouse::Button::Left) leftclicked = false; if (button == Mouse::Button::Right) rightclicked = false; if (button == Mouse::Button::Left) { dbars.mouse_release(); make_sound(); } }; loop = [&]() { while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { switch (event.type) { case sf::Event::KeyPressed: if (event.key.code == sf::Keyboard::Escape) window.close(); treatkeyevent(event.key.code); break; case sf::Event::Closed: window.close(); break; case sf::Event::MouseButtonPressed: mouseclick(event.mouseButton.button); break; case sf::Event::MouseButtonReleased: mouserelease(event.mouseButton.button); break; case sf::Event::MouseMoved: mpos = Vec2(event.mouseMove.x, event.mouseMove.y); mpos_abs = window.mapPixelToCoords(Vec2i(mpos), view); mousemoved(mpos); break; default: treatotherevent(event); break; } } window.clear(background); update(); draw(); window.display(); } }; treatotherevent = [&](Event&e) { if (e.type == Event::MouseWheelMoved && e.mouseWheel.delta) { mpos_abs = window.mapPixelToCoords(Vec2i(mpos), view); //view = window.getView(); if (e.mouseWheel.delta < 0) { zoomlevel *= 2.f; view.setSize(view.getSize()*2.f); view.setCenter(interp(mpos_abs, view.getCenter(), 2.f)); //view.setCenter(interp(mpos_abs, view.getCenter(), 2.f)); } if (e.mouseWheel.delta > 0) { zoomlevel *= 0.5; view.setSize(view.getSize()*.5f); view.setCenter(.5f*(view.getCenter() + mpos_abs)); //view.setCenter(.5f*(view.getCenter() + mpos_abs)); } window.setView(view); } }; #endif // LOOP_LAMBDAS loop(); }
void vsDisplayList::AppendOp(vsDisplayList::op * o) { OpCode type = o->type; switch( type ) { case OpCode_SetColor: SetColor( o->data.GetColor() ); break; case OpCode_SetColors: SetColors( (vsColor*)o->data.p, o->data.GetUInt() ); break; case OpCode_SetColorsBuffer: SetColorsBuffer( (vsRenderBuffer*)o->data.p ); break; case OpCode_VertexArray: VertexArray( (vsVector3D *)o->data.p, o->data.GetUInt() ); break; case OpCode_NormalArray: NormalArray( (vsVector3D *)o->data.p, o->data.GetUInt() ); break; case OpCode_TexelArray: TexelArray( (vsVector2D *)o->data.p, o->data.GetUInt() ); break; case OpCode_ColorArray: ColorArray( (vsColor *)o->data.p, o->data.GetUInt() ); break; case OpCode_VertexBuffer: VertexBuffer( (vsRenderBuffer *)o->data.p ); break; case OpCode_TexelBuffer: TexelBuffer( (vsRenderBuffer *)o->data.p ); break; case OpCode_ColorBuffer: ColorBuffer( (vsRenderBuffer *)o->data.p ); break; case OpCode_BindBuffer: BindBuffer( (vsRenderBuffer *)o->data.p ); break; case OpCode_UnbindBuffer: UnbindBuffer( (vsRenderBuffer *)o->data.p ); break; case OpCode_ClearVertexArray: ClearVertexArray(); break; case OpCode_ClearNormalArray: ClearNormalArray(); break; case OpCode_ClearTexelArray: ClearTexelArray(); break; case OpCode_ClearColorArray: ClearColorArray(); break; case OpCode_ClearArrays: ClearArrays(); break; case OpCode_LineListArray: LineListArray( (int *)o->data.p, o->data.GetUInt() ); break; case OpCode_LineStripArray: LineStripArray( (int *)o->data.p, o->data.GetUInt() ); break; case OpCode_TriangleListArray: TriangleListArray( (int *)o->data.p, o->data.GetUInt() ); break; case OpCode_TriangleStripArray: TriangleStripArray( (int *)o->data.p, o->data.GetUInt() ); break; case OpCode_PointsArray: PointsArray( (int *)o->data.p, o->data.GetUInt() ); break; case OpCode_LineListBuffer: LineListBuffer( (vsRenderBuffer *)o->data.p ); break; case OpCode_LineStripBuffer: LineStripBuffer( (vsRenderBuffer *)o->data.p ); break; case OpCode_TriangleStripBuffer: TriangleStripBuffer( (vsRenderBuffer *)o->data.p ); break; case OpCode_TriangleFanArray: TriangleFanArray( (int *)o->data.p, o->data.GetUInt() ); break; case OpCode_PushTransform: PushTransform( o->data.GetTransform() ); break; case OpCode_SetCameraTransform: SetCameraTransform( o->data.GetTransform() ); break; case OpCode_SetMatrix4x4: SetMatrix4x4( o->data.GetMatrix4x4() ); break; case OpCode_PushMatrix4x4: PushMatrix4x4( o->data.GetMatrix4x4() ); break; case OpCode_SetMatrices4x4: SetMatrices4x4( (vsMatrix4x4*)o->data.p, o->data.i ); break; case OpCode_SetMatrices4x4Buffer: SetMatrices4x4Buffer( (vsRenderBuffer*)o->data.p ); break; case OpCode_SetWorldToViewMatrix4x4: SetWorldToViewMatrix4x4( o->data.GetMatrix4x4() ); break; case OpCode_Set3DProjection: Set3DProjection( o->data.fov, o->data.nearPlane, o->data.farPlane ); break; case OpCode_SetProjectionMatrix4x4: SetProjectionMatrix4x4( o->data.GetMatrix4x4() ); break; case OpCode_PopTransform: PopTransform(); break; case OpCode_EnableStencil: EnableStencil(); break; case OpCode_DisableStencil: DisableStencil(); break; case OpCode_EnableScissor: EnableScissor( o->data.GetBox2D() ); break; case OpCode_DisableScissor: DisableScissor(); break; case OpCode_ClearStencil: ClearStencil(); break; case OpCode_SetViewport: SetViewport( o->data.GetBox2D() ); break; case OpCode_ClearViewport: ClearViewport(); break; case OpCode_Debug: Debug( o->data.GetString() ); break; default: break; } }
int main(int argc, char*argv[]) { #ifndef COMMON_INITS1 cfg.init("bedlab.cfg"); Vec2i windowsize; Vec2i screen_resolution = { int(VideoMode::getDesktopMode().width), int(VideoMode::getDesktopMode().height) }; if (cfg.getvar<int>("auto_winsize")) { auto winscale_which = cfg.getstr("winscale_which"); auto window_scale = cfg.getvar<Vec2>(winscale_which); windowsize = Vec2i(scal(Vec2(screen_resolution), window_scale)); } else { windowsize = cfg.getvar<Vec2i>("windowsize"); } winsize = Vec2(windowsize); UI.init_console(); // ALWAYS AFTER SO IT CAN GET WINDOW SIZE wincenter = 0.5f*Vec2(windowsize); Vec2i windowpos; VideoMode::getDesktopMode().height; if (cfg.getvar<int>("stick_left")) { windowpos = Vec2i( screen_resolution.x - windowsize.x - 10, screen_resolution.y - windowsize.y - 40 ); } else windowpos = (Vec2i(5, 25)); sf::RenderWindow window(sf::VideoMode(windowsize.x, windowsize.y), "bedlab!", 7 //,sf::ContextSettings(0, 0, 1) ); window.setFramerateLimit(cfg.getvar<int>("fps_max")); frame_duration = 1.0f / cfg.getvar<int>("fps_max"); window.setPosition(windowpos); vector<string> keys; auto choice = cfg.getstr("app"); // show_keys(cfg.getstr("app"), keys); #endif // COMMON_INITS1 #ifndef COMMON_INITS2 // we don't have a class/interface/struct with data, everything is local to this function, like a classic stack that all programs are anyway. // Texture cursor_tx; // if (!cursor_tx.loadFromFile(cfg.getstr("cursor"))) // cout << "did not load cursor" << endl; // Sprite cursor; // cursor.setTexture(cursor_tx); // cursor.setOrigin(3, 3); CircleShape cursor = mkcircle({ 0,0 }, Color::Transparent, 3, 1); Color background = cfg.getvar<Color>("background"); window.setMouseCursorVisible(false); Vec2 mpos; bool leftclicked = false, rightclicked = false; // view and zoom View view, ui_view; ui_view = view = window.getDefaultView(); float zoomlevel = 1; Vec2 mpos_abs; float frame_duration = 1.0f / cfg.getvar<int>("fps_max"); float smaller_size = 0.95*min(windowsize.y, windowsize.x); auto all_pt_mult = cfg.getvar<float>("all_pt_mult"); all_pt_size = all_pt_mult * smaller_size; //Transform transf; //transf_glob.translate(smaller_size*0.1, smaller_size*0.5); transf_glob.translate(smaller_size*0.02, smaller_size*0.02); transf_glob.scale(Vec2(smaller_size, smaller_size)); //transf_glob = transf; glob_vert_single = VertexArray(Lines); #endif // COMMON_INITS2 #ifndef COMMON_INITS3 bool point_text = cfg.getvar<int>("street_gen_point_text"); bool alternate_font = cfg.getvar<int>("alternate_font"); glob_vert = vector<VertexArray>(10, VertexArray(LineStrip)); string bar_font; Vec2 barsize; auto winscale_which = cfg.getstr("winscale_which"); if (winscale_which == "winscale_smallest" || winscale_which == "winscale_laptop") { bar_font = UI.small_font;; barsize = { 40,7 }; } else if (winscale_which == "winscale_largest" || winscale_which == "winscale_large") { bar_font = UI.default_font; barsize = { 100,15 }; } else { bar_font = UI.default_font; barsize = { 100,15 }; } //dip_bars dbars(UI.fonts["unaligned"], UI.fontsizes[UI.default_font], { 100,15 }); dip_bars dbars(UI.fonts[bar_font], UI.fontsizes[UI.default_font], barsize, { 1,1 }); // tests in lambdas map<string, function<void(void)>> test_lambdas; VertexArray grid(Lines); auto make_grid = [&](float resolution) { // dummy grid grid.clear(); Color col(64, 64, 64, 32); // glob_vert = vector<VertexArray>(10, VertexArray(Lines)); for (int i = 0; i <= int(resolution); ++i) for (int j = 0; j <= int(resolution); ++j) { grid.append(Vertex(tr(Vec2(i / resolution, 0)), col)); grid.append(Vertex(tr(Vec2(i / resolution, 1)), col)); grid.append(Vertex(tr(Vec2(0, i / resolution)), col)); grid.append(Vertex(tr(Vec2(1, i / resolution)), col)); } }; // actual tests diag_draw ddraw; function<void(void)> test_current = [&] {msg("no test configured"); }; auto street_gen_test = cfg.getstr("street_gen_test"); if (test_lambdas.count(street_gen_test)) { test_current = test_lambdas[street_gen_test]; msgm("ok, using test", street_gen_test); test_current(); } else { msg("invalid test choice string thing bing ding king ping ring sing wing wing"); } #endif // COMMON_INITS3 // █████████████████████ APP ACTUAL █████████████████████ // █████████████████████ callbacks █████████████████████ #ifndef LOOP_LAMBDAS draw = [&]() { window.setView(view); //////////////// OBJECTS THAT CAN ZOOMED //////////////// window.draw(grid); window.draw(glob_vert_single); for (auto&a : glob_pts)window.draw(a); for (auto&a : glob_rects)window.draw(a); for (auto&a : glob_vert)window.draw(a); if (point_text) for (auto&a : glob_texts)window.draw(a); ddraw.draw(window); // UI draw, AFTER ui view and BEFORE other draw window.setView(ui_view); //////////////// OBJECTS THAT CANNOT ZOOMED, MEANING UI //////////////// dbars.draw(window); //br.drawwithtext(window); UI.draw(window); window.draw(cursor); }; update = [&]() { }; treatkeyevent = [&](Keyboard::Key k) { switch (k) { case Keyboard::E: break; case Keyboard::T: point_text ^= 1; break; case Keyboard::Q: break; case Keyboard::BackSpace: glob_pts.clear(); glob_texts.clear(); glob_rects.clear(); glob_vert.clear(); break; case Keyboard::Space: break; case Keyboard::S: screenshot(window); break; case Keyboard::Num1: case Keyboard::Num2: case Keyboard::Num3: case Keyboard::Num4: case Keyboard::Num5: break; } }; mousemoved = [&](Vec2 pos) { dbars.mouse_moved(mpos); cursor.setPosition(pos); if (leftclicked); }; mouseclick = [&](sf::Mouse::Button button) { if (button == Mouse::Button::Left) leftclicked = true; if (button == Mouse::Button::Right) rightclicked = true; if (button == Mouse::Button::Left) { dbars.mouse_click(mpos); //test_search(); //test_inter(); } if (button == Mouse::Button::Right) { //test_inter(); } }; mouserelease = [&](sf::Mouse::Button button) { if (button == Mouse::Button::Left) leftclicked = false; if (button == Mouse::Button::Right) rightclicked = false; dbars.mouse_release(); test_current(); }; loop = [&]() { while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { switch (event.type) { case sf::Event::KeyPressed: if (event.key.code == sf::Keyboard::Escape) window.close(); treatkeyevent(event.key.code); break; case sf::Event::Closed: window.close(); break; case sf::Event::MouseButtonPressed: mouseclick(event.mouseButton.button); break; case sf::Event::MouseButtonReleased: mouserelease(event.mouseButton.button); break; case sf::Event::MouseMoved: mpos = Vec2(event.mouseMove.x, event.mouseMove.y); mpos_abs = window.mapPixelToCoords(Vec2i(mpos), view); mousemoved(mpos); break; default: treatotherevent(event); break; } } window.clear(background); update(); draw(); //static bool once = true; //if (once) { // screenshot(window); // once = false; //} window.display(); } }; treatotherevent = [&](Event&e) { if (e.type == Event::MouseWheelMoved && e.mouseWheel.delta) { mpos_abs = window.mapPixelToCoords(Vec2i(mpos), view); //view = window.getView(); if (e.mouseWheel.delta < 0) { zoomlevel *= 2.f; view.setSize(view.getSize()*2.f); view.setCenter(interp(mpos_abs, view.getCenter(), 2.f)); //view.setCenter(interp(mpos_abs, view.getCenter(), 2.f)); } if (e.mouseWheel.delta > 0) { zoomlevel *= 0.5; view.setSize(view.getSize()*.5f); view.setCenter(.5f*(view.getCenter() + mpos_abs)); //view.setCenter(.5f*(view.getCenter() + mpos_abs)); } window.setView(view); } }; #endif // LOOP_LAMBDAS loop(); }
void game :: init(){ pause_drawing=false; glines = VertexArray(Lines); conf.init("conf.cfg"); cfg=&conf; fps_coeff = 1.f/cfg->getfloat("timestep"); //srand((unsigned)time(0)); window.setKeyRepeatEnabled(false); wincenter.x=window.getSize().x/2; wincenter.y=window.getSize().y/2; winsize.x=window.getSize().x; winsize.y=window.getSize().y; window.setFramerateLimit(fps_coeff); if(cfg->getint("stick_left")) { Vec2i screen_resolution; cfg->SET(screen_resolution); #ifdef __APPLE__ screen_resolution=Vec2i(1280,800); #endif Vec2i windowpos(screen_resolution.x - winsize.x-10,25); cout << strfy(screen_resolution) << endl; cout << strfy(windowpos) << endl; window.setPosition(windowpos); } else window.setPosition(Vec2i(5,25)); { Vec2 cons_view_size; cfg->SET(cons_view_size); console_view.setSize(Vec2(cons_view_size.x*wsize.x,cons_view_size.y*wsize.y)); fixedview = window.getDefaultView(); mainview=fixedview; } wsize = winsize; wcenter = wincenter; float window_coeff = conf.getfloat("window_coeff"); if(window_coeff!=1.0) { Vec2u resized(winsize.x*window_coeff,winsize.y*window_coeff); window.setSize(resized); } bg = cfg->getcolor("background"); string path="yes/"; #ifdef __APPLE__ path = resourcePath()+path; #endif if(!cursor_tx.loadFromFile(path+"cursor4.png")) cout << "did not load cursor" << endl; cursor.setTexture(cursor_tx); cursor.setOrigin(3,3); window.setMouseCursorVisible(false); confpoint(c1,3,Color::Green); confpoint(c2,3,Color::Red); // fonts and f*****g globals init_font(); font1 = &ft; font2 = &ft2; charsize=chsz; charsize2=chsz2; // console console = new sfconsole; console->numlines=conf.getint("numlines"); console->init(); cons=console; draw_console = true; labs=new labels; labs->pos=Vec2(15,winsize.y-30); lbs=&(labs->strings); win=&window; add_symbol_to_lambdas(); if(cfg->getint("use_menu")) { menu_loop(); } else { init_gameobj(); loop(); } }