|
| 1 | +/* |
| 2 | + * Gamedev Framework (gf) |
| 3 | + * Copyright (C) 2016-2021 Julien Bernard |
| 4 | + * |
| 5 | + * This software is provided 'as-is', without any express or implied |
| 6 | + * warranty. In no event will the authors be held liable for any damages |
| 7 | + * arising from the use of this software. |
| 8 | + * |
| 9 | + * Permission is granted to anyone to use this software for any purpose, |
| 10 | + * including commercial applications, and to alter it and redistribute it |
| 11 | + * freely, subject to the following restrictions: |
| 12 | + * |
| 13 | + * 1. The origin of this software must not be misrepresented; you must not |
| 14 | + * claim that you wrote the original software. If you use this software |
| 15 | + * in a product, an acknowledgment in the product documentation would be |
| 16 | + * appreciated but is not required. |
| 17 | + * 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | + * misrepresented as being the original software. |
| 19 | + * 3. This notice may not be removed or altered from any source distribution. |
| 20 | + */ |
| 21 | +#include <cstdlib> |
| 22 | +#include <iostream> |
| 23 | + |
| 24 | +#include <gf/Color.h> |
| 25 | +#include <gf/Event.h> |
| 26 | +#include <gf/LightSystem.h> |
| 27 | +#include <gf/LightTextures.h> |
| 28 | +#include <gf/RenderWindow.h> |
| 29 | +#include <gf/Views.h> |
| 30 | +#include <gf/ViewContainer.h> |
| 31 | +#include <gf/Window.h> |
| 32 | + |
| 33 | +int main() { |
| 34 | + static constexpr gf::Vector2i ScreenSize(640, 480); |
| 35 | + |
| 36 | + gf::Window window("45_lights", ScreenSize); |
| 37 | + gf::RenderWindow renderer(window); |
| 38 | + |
| 39 | + gf::ViewContainer views; |
| 40 | + |
| 41 | + gf::ExtendView view; |
| 42 | + view.setCenter({ 0.0f, 0.0f }); |
| 43 | + view.setSize({ 1000.0f, 1000.0f }); |
| 44 | + views.addView(view); |
| 45 | + |
| 46 | + views.setInitialFramebufferSize(ScreenSize); |
| 47 | + |
| 48 | + gf::LightSystem lights(ScreenSize); |
| 49 | + |
| 50 | + std::cout << "Gamedev Framework (gf) example #45: Lights\n"; |
| 51 | + |
| 52 | + gf::RectF red = gf::RectF::fromPositionSize({ 0.0f, 0.0f }, { 128.0f, 128.0f }); |
| 53 | + gf::RectF green = gf::RectF::fromPositionSize({ 128.0f, 0.0f }, { 128.0f, 128.0f }); |
| 54 | + gf::CircF blue({ -200, -200 }, 32); |
| 55 | + gf::Vector2f pos0 = { 0.0f, -200.0f }; |
| 56 | + gf::Vector2f pos1 = { 250.0f, 250.0f }; |
| 57 | + |
| 58 | + static constexpr int Size = 1024; |
| 59 | + |
| 60 | + gf::Texture texture = gf::LightTextures::createRealisticLight(Size, 1.0f, Size / 40); |
| 61 | + |
| 62 | + gf::LightPointEmission light0(texture); |
| 63 | + light0.setPosition(pos0); |
| 64 | + light0.setAnchor(gf::Anchor::Center); |
| 65 | + light0.setSourceRadius(Size * 0.05f); |
| 66 | + light0.setShadowOverExtendMultiplier(1.5f); |
| 67 | + light0.setLocaleCastCenter({ 0.0f, 0.0f }); // gf::vec(Size * 0.5f, Size * 0.5f)); |
| 68 | + lights.addLightPoint(light0); |
| 69 | + |
| 70 | + gf::LightPointEmission light1(texture); |
| 71 | + light1.setPosition(pos1); |
| 72 | + light1.setAnchor(gf::Anchor::Center); |
| 73 | + light1.setSourceRadius(Size * 0.01f); |
| 74 | + light1.setShadowOverExtendMultiplier(20.0f); |
| 75 | + light1.setLocaleCastCenter({ 0.0f, 0.0f }); // gf::vec(Size * 0.5f, Size * 0.5f)); |
| 76 | + lights.addLightPoint(light1); |
| 77 | + |
| 78 | + gf::CircleShape circle(light0.getSourceRadius()); |
| 79 | + circle.setColor(gf::Color::Transparent); |
| 80 | + circle.setOutlineColor(gf::Color::Red); |
| 81 | + circle.setOutlineThickness(2.0f); |
| 82 | + circle.setPosition(pos0); |
| 83 | + circle.setAnchor(gf::Anchor::Center); |
| 84 | + |
| 85 | + |
| 86 | + gf::LightShape occluder0(red); |
| 87 | + lights.addLightShape(occluder0); |
| 88 | + |
| 89 | + gf::RectangleShape shape0(red); |
| 90 | + shape0.setColor(gf::Color::Red); |
| 91 | + |
| 92 | + gf::LightShape occluder1(blue); |
| 93 | + lights.addLightShape(occluder1); |
| 94 | + |
| 95 | + gf::CircleShape shape1(blue); |
| 96 | + shape1.setColor(gf::Color::Blue); |
| 97 | + |
| 98 | + gf::LightShape occluder2(green); |
| 99 | + lights.addLightShape(occluder2); |
| 100 | + |
| 101 | + gf::RectangleShape shape2(green); |
| 102 | + shape2.setColor(gf::Color::Green); |
| 103 | + |
| 104 | + |
| 105 | + renderer.clear(gf::Color::Chartreuse); |
| 106 | + |
| 107 | + bool change = false; |
| 108 | + |
| 109 | + while (window.isOpen()) { |
| 110 | + gf::Event event; |
| 111 | + |
| 112 | + while (window.pollEvent(event)) { |
| 113 | + switch (event.type) { |
| 114 | + case gf::EventType::Closed: |
| 115 | + window.close(); |
| 116 | + break; |
| 117 | + |
| 118 | + case gf::EventType::KeyPressed: |
| 119 | + if (event.key.keycode == gf::Keycode::F1) { |
| 120 | + lights.dump(); |
| 121 | + } |
| 122 | + break; |
| 123 | + |
| 124 | + case gf::EventType::MouseButtonPressed: |
| 125 | + change = true; |
| 126 | + pos0 = renderer.mapPixelToCoords(event.mouseButton.coords); |
| 127 | + light0.setPosition(pos0); |
| 128 | + circle.setPosition(pos0); |
| 129 | + break; |
| 130 | + |
| 131 | + case gf::EventType::MouseButtonReleased: |
| 132 | + change = false; |
| 133 | + break; |
| 134 | + |
| 135 | + case gf::EventType::MouseMoved: |
| 136 | + if (change) { |
| 137 | + pos0 = renderer.mapPixelToCoords(event.mouseCursor.coords); |
| 138 | + light0.setPosition(pos0); |
| 139 | + circle.setPosition(pos0); |
| 140 | + } |
| 141 | + break; |
| 142 | + |
| 143 | + default: |
| 144 | + break; |
| 145 | + } |
| 146 | + |
| 147 | + views.processEvent(event); |
| 148 | + } |
| 149 | + |
| 150 | + renderer.setView(view); |
| 151 | + renderer.clear(); |
| 152 | + renderer.draw(shape0); |
| 153 | + renderer.draw(shape1); |
| 154 | + renderer.draw(shape2); |
| 155 | + renderer.draw(lights); |
| 156 | + renderer.draw(circle); |
| 157 | + renderer.display(); |
| 158 | + } |
| 159 | + |
| 160 | + return 0; |
| 161 | +} |
0 commit comments