Browse Source

Tut 9

master
Georg Spar 2 years ago
commit
091e7faaf5
  1. 63
      CMakeLists.txt
  2. 17
      game.cpp
  3. 13
      game.h
  4. 15
      main.cpp
  5. 5
      res.qrc
  6. 33
      tower.cpp
  7. 14
      tower.h
  8. BIN
      tower.png
  9. 15
      widget.cpp
  10. 21
      widget.h
  11. 19
      widget.ui

63
CMakeLists.txt

@ -0,0 +1,63 @@
cmake_minimum_required(VERSION 3.5)
project(TowerDefense VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
set(PROJECT_SOURCES
main.cpp
tower.h tower.cpp
game.h game.cpp
res.qrc
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(TowerDefense
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET TowerDefense APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(TowerDefense SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(TowerDefense
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(TowerDefense PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
set_target_properties(TowerDefense PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
install(TARGETS TowerDefense
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(TowerDefense)
endif()

17
game.cpp

@ -0,0 +1,17 @@
#include "game.h"
#include <QGraphicsScene>
#include "tower.h"
Game::Game() {
scene = new QGraphicsScene(this);
setScene(scene);
// create a tower
Tower *t = new Tower();
// add tower
scene->addItem(t);
setFixedSize(800,600);
}

13
game.h

@ -0,0 +1,13 @@
#ifndef GAME_H
#define GAME_H
#include <QGraphicsView>
class Game : public QGraphicsView
{
public:
Game();
QGraphicsScene *scene;
};
#endif // GAME_H

15
main.cpp

@ -0,0 +1,15 @@
#include "game.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Game *game = new Game();
game->show();
return a.exec();
}

5
res.qrc

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/images">
<file>tower.png</file>
</qresource>
</RCC>

33
tower.cpp

@ -0,0 +1,33 @@
#include "tower.h"
#include <QPixmap>
#include <QVector>
#include <QPointF>
Tower::Tower(QGraphicsItem *parent) {
setPixmap(QPixmap(":/images/tower.png"));
// create points vectors
QVector<QPointF> points;
points << QPoint(1,0) << QPoint(2,0) << QPoint(3,1) << QPoint(3,2) << QPoint(2,3) << QPoint(1,3) << QPoint(0,2) << QPoint(0,1);
// scale points
int SCALE_FACTOR = 70;
for (size_t i = 0, n = points.size(); i <n; i++) {
points[i] *= SCALE_FACTOR;
}
// create a polygon from these points
QPolygonF polygon(points);
// create the QGraphicsPolygonItem
attack_area = new QGraphicsPolygonItem(QPolygonF(points), this);
// move the polygon
QPointF poly_center(1.5, 1.5);
poly_center *= SCALE_FACTOR;
poly_center = mapToScene(poly_center);
QPointF tower_center(x()+34, y()+64);
QLineF ln(poly_center, tower_center);
attack_area->setPos(x()+ln.dx(), y()+ln.dy());
}

14
tower.h

@ -0,0 +1,14 @@
#ifndef TOWER_H
#define TOWER_H
#include <QGraphicsPixmapItem>
#include <QGraphicsPolygonItem>
class Tower : public QGraphicsPixmapItem {
public:
Tower(QGraphicsItem *parent=0);
private:
QGraphicsPolygonItem *attack_area;
};
#endif // TOWER_H

BIN
tower.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

15
widget.cpp

@ -0,0 +1,15 @@
#include "widget.h"
#include "./ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}

21
widget.h

@ -0,0 +1,21 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H

19
widget.ui

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
</widget>
<resources/>
<connections/>
</ui>
Loading…
Cancel
Save