int main(int argc, char *argv[]) { Q_UNUSED(argc) Q_UNUSED(argv) ////////////////////////////////////////////////////////////////// /// Init GraphicSystem singleton GraphicSystem::initialize("GraphicSystemTest", "data/fonts/DejaVuSans.ttf"); ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// /// Get pointers of GraphicSystem sf::RenderWindow *window; window = GraphicSystem::instance()->getWindow(); tgui::Gui *gui; gui = GraphicSystem::instance()->getGUI(); ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// /// Create widgets tgui::Picture::Ptr picture((*gui)); picture->load("data/others/Linux.jpg"); tgui::Button::Ptr button((*gui)); button->load(THEME_CONFIG_FILE); button->setPosition(40, 25); button->setText("Quit"); button->setCallbackId(1); button->bindCallback(tgui::Button::LeftMouseClicked); button->setSize(300, 40); tgui::ChatBox::Ptr chatbox((*gui)); chatbox->load(THEME_CONFIG_FILE); chatbox->setSize(200, 100); chatbox->setTextSize(20); chatbox->setPosition(400, 25); chatbox->addLine("Line 1", sf::Color::Red); chatbox->addLine("Line 2", sf::Color::Blue); chatbox->addLine("Line 3", sf::Color::Green); chatbox->addLine("Line 4", sf::Color::Yellow); chatbox->addLine("Line 5", sf::Color::Cyan); chatbox->addLine("Line 6", sf::Color::Magenta); tgui::Checkbox::Ptr checkbox((*gui)); checkbox->load(THEME_CONFIG_FILE); checkbox->setPosition(40, 80); checkbox->setText("Checkbox"); checkbox->setSize(32, 32); tgui::ChildWindow::Ptr child((*gui)); child->load(THEME_CONFIG_FILE); child->setSize(200, 100); child->setBackgroundColor(sf::Color(80, 80, 80)); child->setPosition(400, 460); child->setTitle("Child window"); child->setIcon("data/others/icon.jpg"); tgui::ComboBox::Ptr comboBox((*gui)); comboBox->load(THEME_CONFIG_FILE); comboBox->setSize(120, 21); comboBox->setPosition(210, 440); comboBox->addItem("Item 1"); comboBox->addItem("Item 2"); comboBox->addItem("Item 3"); comboBox->setSelectedItem("Item 2"); tgui::EditBox::Ptr editBox((*gui)); editBox->load(THEME_CONFIG_FILE); editBox->setPosition(40, 200); editBox->setSize(300, 30); tgui::Label::Ptr label((*gui)); label->load(THEME_CONFIG_FILE); label->setText("Label"); label->setPosition(40, 160); label->setTextColor(sf::Color(200, 200, 200)); label->setTextSize(24); tgui::ListBox::Ptr listBox((*gui)); listBox->load(THEME_CONFIG_FILE); listBox->setSize(150, 120); listBox->setItemHeight(20); listBox->setPosition(40, 440); listBox->addItem("Item 1"); listBox->addItem("Item 2"); listBox->addItem("Item 3"); tgui::LoadingBar::Ptr loadingbar((*gui)); loadingbar->load(THEME_CONFIG_FILE); loadingbar->setPosition(40, 330); loadingbar->setSize(300, 30); loadingbar->setValue(35); tgui::MenuBar::Ptr menu((*gui)); menu->load(THEME_CONFIG_FILE); menu->setSize(window->getSize().x, 20); menu->addMenu("File"); menu->addMenuItem("File", "Load"); menu->addMenuItem("File", "Save"); menu->addMenuItem("File", "Exit"); menu->bindCallback(tgui::MenuBar::MenuItemClicked); menu->setCallbackId(2); sf::Texture texture; texture.loadFromFile("data/others/ThinkLinux.jpg"); tgui::Panel::Ptr panel((*gui)); panel->setSize(200, 140); panel->setPosition(400, 150); panel->setBackgroundTexture(&texture); tgui::RadioButton::Ptr radioButton((*gui)); radioButton->load(THEME_CONFIG_FILE); radioButton->setPosition(40, 120); radioButton->setText("Radio Button"); radioButton->setSize(32, 32); tgui::Slider::Ptr slider((*gui)); slider->load(THEME_CONFIG_FILE); slider->setVerticalScroll(false); slider->setPosition(40, 250); slider->setSize(300, 25); slider->setValue(2); tgui::Scrollbar::Ptr scrollbar((*gui)); scrollbar->load(THEME_CONFIG_FILE); scrollbar->setVerticalScroll(false); scrollbar->setPosition(40, 290); scrollbar->setSize(300, 25); scrollbar->setMaximum(5); scrollbar->setLowValue(3); tgui::Slider2d::Ptr slider2d((*gui)); slider2d->load("data/widgets/Slider2d/Black.conf"); slider2d->setPosition(400, 300); slider2d->setSize(200, 150); tgui::SpinButton::Ptr spinButton((*gui)); spinButton->load(THEME_CONFIG_FILE); spinButton->setPosition(40, 410); spinButton->setVerticalScroll(false); spinButton->setSize(40, 20); tgui::SpriteSheet::Ptr spritesheet((*gui)); spritesheet->load("data/others/ThinkLinux.jpg"); spritesheet->setCells(4, 4); spritesheet->setVisibleCell(2, 3); spritesheet->setSize(160, 120); spritesheet->setPosition(620, 25); tgui::Tab::Ptr tab((*gui)); tab->load(THEME_CONFIG_FILE); tab->setPosition(40, 370); tab->add("Item 1"); tab->add("Item 2"); tab->add("Item 3"); tgui::TextBox::Ptr textBox((*gui)); textBox->load(THEME_CONFIG_FILE); textBox->setPosition(210, 470); textBox->setSize(180, 120); textBox->setTextSize(16); comboBox->moveToFront(); ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// /// Start main loop while(window->isOpen()) { GraphicSystem::instance()->injectPreUpdate(0); sf::Event event; while(GraphicSystem::instance()->pollWindowEvent(event)) { if(event.type == sf::Event::Closed) window->close(); GraphicSystem::instance()->handleGUIEvent(event); } tgui::Callback callback; while(GraphicSystem::instance()->pollGUICallback(callback)) { if(callback.id == 1) window->close(); else if(callback.id == 2) { if(callback.text == "Exit") window->close(); } } GraphicSystem::instance()->injectPostUpdate(0); } ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// /// Destroy GraphicSystem GraphicSystem::shutdown(); ////////////////////////////////////////////////////////////////// return 0; }
bool EditorObject::build(ResourceFile & resources) { // create the container to hold the graphic ids delete m_compiledGraphics; m_compiledGraphics = new Array3<QString>(tileCountX(), tileCountY(), layerCount()); Array3< QList<QImage> * > animations(tileCountX(), tileCountY(), layerCount()); // initialize animation list for (int z=0; z<layerCount(); z++) { for (int y=0; y<tileCountY(); ++y) { for (int x=0; x<tileCountX(); ++x) { animations.set(x,y,z,new QList<QImage>()); } } } // find out the greatest frames per second int fps = 0; for (int layerIndex=0; layerIndex<m_graphics.size(); ++layerIndex) { QList<ObjectGraphic *> * layer = m_graphics.at(layerIndex); for (int i=0; i<layer->size(); ++i) { ObjectGraphic * graphicInstance = layer->at(i); if (graphicInstance->graphic->framesPerSecond() > fps) fps = graphicInstance->graphic->framesPerSecond(); } } // for each animation frame int frame = 0; while (true) { if (frame > 0) { // check if the animation has looped yet. bool firstFrame = true; for (int layerIndex=0; layerIndex<m_graphics.size(); ++layerIndex) { QList<ObjectGraphic *> * layer = m_graphics.at(layerIndex); for (int i=0; i<layer->size(); ++i) { ObjectGraphic * graphicInstance = layer->at(i); if ((frame+1) % graphicInstance->graphic->frameCount() != 0) { firstFrame = false; break; } } if (! firstFrame) break; } // this means all animations have looped and we're done // collecting frames if (firstFrame) break; } for (int z=0; z<layerCount(); ++z) { // render the layer once into a QImage QImage layerImage(tileCountX() * Tile::sizeInt, tileCountY() * Tile::sizeInt, QImage::Format_ARGB32); layerImage.fill(Qt::transparent); QPainter layerPainter(&layerImage); render(layerPainter, z, frame, fps); // for each tile, draw that tile into a different QImage for (int y=0; y<tileCountY(); ++y) { for (int x=0; x<tileCountX(); ++x) { QImage tileImage(Tile::sizeInt, Tile::sizeInt, QImage::Format_ARGB32); tileImage.fill(Qt::transparent); QPainter p(&tileImage); p.drawImage(0, 0, layerImage, x * Tile::sizeInt, y * Tile::sizeInt, Tile::sizeInt, Tile::sizeInt); QList<QImage> * frames = animations.get(x,y,z); frames->append(tileImage); } } } ++frame; } int frameCount = frame; // create a spritesheet for each tile for (int z=0; z<layerCount(); ++z) { for (int y=0; y<tileCountY(); ++y) { for (int x=0; x<tileCountX(); ++x ) { // create the spritesheet QList<QImage> * frames = animations.get(x,y,z); QImage spritesheet(Tile::sizeInt * frameCount, Tile::sizeInt, QImage::Format_ARGB32); spritesheet.fill(Qt::transparent); QPainter p(&spritesheet); for (int i=0; i<frameCount; ++i) p.drawImage(i * Tile::sizeInt, 0, frames->at(i % frames->size())); // create the binary data QByteArray tile; // magic character tile.append("G"); int codeVersion = 1; tile.append((char *) &codeVersion, 4); int graphicType = Graphic::gtAnimation; tile.append((char *) &graphicType, 4); int storageType = Graphic::stPNG; tile.append((char *) &storageType, 4); // color key: hardcode magenta char red = 255; char green = 0; char blue = 255; tile.append(&red, 1); tile.append(&green, 1); tile.append(&blue, 1); tile.append((char *) &frameCount, 4); tile.append((char *) &fps, 4); // frame width and height tile.append((char *) &Tile::sizeInt, 4); tile.append((char *) &Tile::sizeInt, 4); // save spritesheet into memory QByteArray imageData; QBuffer buffer(&imageData); buffer.open(QIODevice::WriteOnly); spritesheet.save(&buffer, "PNG"); int imageDataSize = imageData.size(); tile.append((char *) &imageDataSize, 4); tile.append(imageData); // come up with a name QString dash = "-"; QString ext = ".ani"; QString graphicName = m_name + dash + QString::number(x) + dash + QString::number(y) + dash + QString::number(z) + ext; resources.updateResource(graphicName.toStdString(), tile.constData(), tile.size()); m_compiledGraphics->set(x,y,z,graphicName); } } } // cleanup animation list for (int z=0; z<layerCount(); z++) { for (int y=0; y<tileCountY(); ++y) { for (int x=0; x<tileCountX(); ++x) { delete animations.get(x,y,z); } } } return true; }