So here's my issue. I'm making an extremely simple game in Qt, but I'm having trouble increasing the difficulty in the game. For example, once your score hits a certain value, you level up. In game the text changes and says Level 2, but the game doesn't get any harder. I have the idea to achieve this by making the enemies move faster. I have the enemies moving at a predefined value, but I want the position change to be equal to the value "speed" in Level.cpp. The answer is probably staring me in the face, but it's the simplest answers that always get me. Below is a header and 2 source files that connect Level.cpp and Enemy.cpp.
Enemy.cpp
#include "Enemy.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include <stdlib.h>
#include "Game.h"
#include "Level.h"
extern Game * game;
Enemy::Enemy(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){
//set random x position
int random_number = rand() % 700;
setPos(random_number,0);
// drew the rect
setPixmap(QPixmap(":/images/enemy.png"));
setTransformOriginPoint(50,50);
// make/connect a timer to move() the enemy every so often
QTimer * timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
// start the timer
timer->start(30);
}
void Enemy::move(){
// move enemy down
setPos(x(),y()+7);
//Instead of 7 above, use speed from Level.cpp
// destroy enemy when it goes out of the screen
if (pos().y() > 600){
//decrease the health
game->health->decrease();
scene()->removeItem(this);
delete this;
}
}
Level.cpp
#include "Level.h"
#include "Enemy.h"
#include <QFont>
Level::Level(QGraphicsItem *parent): QGraphicsTextItem(parent){
// initialize the score to 0
level = 1;
speed = 5;
// draw the text
setPlainText(QString("Level: ") + QString::number(level)); // Level: 0
setDefaultTextColor(Qt::green);
setFont(QFont("times",16));
}
void Level::LevelUp(){
level++;
speed = speed+2;
setPlainText(QString("Level: ") + QString::number(level)); // Level: 1
}
int Level::getLevel(){
return level;
return speed;
}
Level.h
#ifndef LEVEL_H
#define LEVEL_H
#include <QGraphicsTextItem>
class Level: public QGraphicsTextItem{
public:
Level(QGraphicsItem * parent=0);
int getLevel();
void LevelUp();
int speed; //speed is public because I got "int is private" errors on a few things I tried.
private:
int level;
};
#endif // LEVEL_H
Thanks for any help, and if possible in-depth explanations on what you did to solve the issue.
EDIT: Would my issue be trying to send out Speed, but being unable to because the Level function is labled as a QGraphicsTextItem? Will I just need to create a separate function to increase speed?
not sure which design pattern you are using, but sounds you need a controller to control enemy object. So basically whenever level is changed the level obj should tell the controller, well now enemy should increase speed. Then the controller will notify the enemy.
Related
header
#ifndef AUDIORECORD_H
#define AUDIORECORD_H
#include <QMediaRecorder>
#include <QAudioRecorder>
#include <QUrl>
class AudioRecorder : public QAudioRecorder
{
Q_OBJECT
public:
AudioRecorder(QObject * parent);
~AudioRecorder(){}
};
#endif // AUDIORECORD_H
source
#include "audiorecord.h"
#include<iostream>
using namespace std;
AudioRecorder::AudioRecorder(QObject * parent = 0)
{
this->setOutputLocation(QUrl::fromLocalFile("test.mp3"));
int x = 0;
while ( x > 10000)
{
this->record();
x++;
}
this->stop();
std::cout<<"\ndsffsdf\n";
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "audiorecord.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QObject p;
AudioRecorder obj(&p);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
This does not produce any error but it does not record also. I am not expecting any quality or full fledged recording.
I just want to see how this recorder works.
That test.mp3 is not getting saved.
What bare minimum can I add or subtract to it to make it record something and save?
Cause
What would be if I tell you 10 000 times: Go buy some milk, while firmly holding your hand, then just before I let you go to say: Ah, forget it? Would you be able to buy the milk?
You are doing the same with your code:
int x = 0;
while ( x > 10000)
{
this->record();
x++;
}
this->stop();
You are calling 10 000 times QAudioRecorder::record, but you do not let Qt get to the event loop and actualy execute your command. Then just before Qt gets to the event loop, you say: stop.
Solution
First of all, you do not need to subclass QAudioRecorder, because you do not add any new functionality to it. Just create an instance of the class and use it.
Second, record and stop are slots. Connect them to the clicked signal of the corresponding push buttons in you GUI, e.g.:
auto *audioRecorder = new QAudioRecorder(this);
...
connect(btnRecord, &QPushButton::clicked, audioRecorder, &QAudioRecorder::record);
connect(btnStop, &QPushButton::clicked, audioRecorder, &QAudioRecorder::stop);
Note: For more information, please take a look at the example from the documentation.
It's telling me that "accelerometer" and "lcd" were not declared in this scope when they're used in PIT0_IRQHandler. None of the solutions I've found online have worked as of yet so I figured I'd just post it up. I'm pretty new to this as well so feel free to point out any and every error included
#include <stdio.h>
#include "system.h"
#include "derivative.h"
#include "hardware.h"
#include "delay.h"
#include "lcd.h"
#include "i2c.h"
#include "level.h"
#include "mma845x.h"
#include "tone.h"
#include "accel.h"
/// LCD dimensions and centerpoint
#define LCD_WIDTH (LCD_X_MAX-LCD_X_MIN)
#define LCD_HEIGHT (LCD_Y_MAX-LCD_Y_MIN)
#define CENTRE_X ((LCD_X_MAX-LCD_X_MIN)/2)
#define CENTRE_Y ((LCD_Y_MAX-LCD_Y_MIN)/2)
#define BACKGROUND_COLOUR (RED)
#define FOREGROUND_COLOUR (WHITE)
#define CIRCLE_RADIUS (20)
using namespace USBDM;
void initialisePIT(int channel, uint32_t interval) {
// Enable clock to PIT
SIM->SCGC6 |= SIM_SCGC6_PIT_MASK;
// Enable PIT module
PIT->MCR = PIT_MCR_FRZ_MASK;
// Set reload value
PIT->CHANNEL[channel].LDVAL = interval-1;
// Enable this channel with interrupts
PIT->CHANNEL[channel].TCTRL = PIT_TCTRL_TEN_MASK|PIT_TCTRL_TIE_MASK;
// Enable PITn interrupts in NVIC
NVIC_EnableIRQ((IRQn_Type)(PIT0_IRQn+channel));
// Set arbitrary priority level
NVIC_SetPriority((IRQn_Type)(PIT0_IRQn+channel), 8);
}
void initialiseLCD(){
// Instantiate SPI interface class
Spi *spi = new Spi0();
// Instantiate LCD interface class
Lcd *lcd = new Lcd(spi);
// Clear Background and set to a preset (Set above) colour
lcd->clear(BACKGROUND_COLOUR);
// Draw crosshairs and initial circle on the screen
lcd->drawLine(LCD_WIDTH/2,LCD_WIDTH/2,0,LCD_HEIGHT,BLACK);
lcd->drawLine(0,LCD_WIDTH,LCD_HEIGHT/2,LCD_HEIGHT/2,BLACK);
lcd->drawCircle(CENTRE_X, CENTRE_Y, 20, WHITE);
}
void initialiseAccel()
{
I2c *i2c = new I2c0();
MMA845x *accelerometer = new MMA845x(i2c, MMA845x::ACCEL_2Gmode);
}
void PIT0_IRQHandler(void) {
int accelStatus;
int16_t accelX,accelY,accelZ;
accelerometer->readAccelerometerXYZ(&accelStatus, &accelX, &accelY, &accelZ);
lcd->clear(BACKGROUND_COLOUR);
lcd->drawCircle(accelX,accelY,20,WHITE);
// Clear the interrupt request from PIT
PIT->CHANNEL[0].TFLG = PIT_TFLG_TIF_MASK;
}
accelerometer is declared in initialiseAccel. It will go out of scope at the end of that function, and is not a known name in PIT0_IRQHandler. You should probably create a class to hold those variables and functions, or you'll have to use global variables (possibly in a namespace) to hold those values.
#include "IRSensor.h"
#include "Turret.h"
#include "StepperButtonController.h"
#include "LoadBottleButton.h"
LoadBottleButton go(A3,1000);
void setup()
{
Serial.begin(9600);
Serial.println("Port Open");
}
void loop()
{
if(go.Read())
{
go.Monitor();
}
}
Above is the Main code
#ifndef LoadBottleButton_cpp
#define LoadBottleButton_cpp
#include "Arduino.h"
#include "ScaleObject.h"
#include "LoadBottleButton.h"
#include "Turret.h"
#include "StepperButtonController.h"
ScaleObject* so;
Turret* tPointer;
LoadBottleButton::LoadBottleButton(int pin, int debounce):StepperButtonController(pin,debounce)
{
}
void LoadBottleButton::Monitor()
{
Serial.println("In Monitor");
while(tPointer->getTurret().BottleCenterState==false)
{
Serial.println("In Monitor While Loop");
tPointer->Start();
SETUP = true;
load = true;
unload = !so->getScale().Empty();
Serial.println(load);
Serial.println(unload);
Serial.println(!so->getScale().Empty());
if(unload)
{
unload=!so->getScale().Empty();
}
else if(load && !so->getScale().Empty())
{
load = !tPointer->BottleCentered();
}
if(!load && !unload && SETUP)
{
tPointer->Stop();
SETUP = false;
}
}
}
#endif
And above is the LoadBottleButtonClass.cpp file.
#ifndef Turret_cpp
#define Turret_cpp
//#include "HX711.h"
#include "Turret.h"
#include "Arduino.h"
#include "StepperButtonController.h"
#include "ScaleObject.h"
#include "IRSensor.h"
//StepperButtonController Clear(9,1000);
void StepTurret();
Turret turret(2,3,4,StepTurret);
void StepTurret()
{
turret.Step();
}
ScaleObject* tso;
IRSensor* irs;
Turret::Turret()
{
}
Turret Turret::getTurret()
{
return turret;
}
Turret::Turret(int en, int dir, int clk, void(*stepFunction)()):stepper2(en,dir,clk,stepFunction)
{
tso->getScale().tare();
tso->getScale().set_gain(128);
tso->getScale().set_scale(-3483.4);
}
void Turret::SeekBottleCenter()
{
Start();
while(irs->IRState()==1)
{
Serial.println("High");
Serial.println(irs->IRState());
}
while(irs->IRState()==0)
{
Serial.println("Low");
}
}
bool Turret::BottleCentered()
{
return turret.BottleCenterState;
}
void Turret::ClearFunction()
{
wt = tso->getScale().get_units();
while(wt>5)
{
Serial.println("Clearing");
wt = tso->getScale().get_units();
Rotate(20);
}
}
#endif
And above is the Turret.cpp file.
#ifndef IRSensor_cpp
#define IRSensor_cpp
#include "Arduino.h"
#include "IRSensor.h"
IRSensor i(5);
IRSensor::IRSensor(int pin)
{
IRSensorPin = pin;
pinMode(pin,INPUT);
}
int IRSensor::IRState()
{
return digitalRead(i.IRSensorPin);
}
#endif
And above is the IRSensor.cpp file. So essentially I press the go button declared in my main, that button calls monitor in the LoadBottleButton.cpp file, that method uses a turret point to get access to the Turret.cpp methods and a boolean named BottleCenterState. But the code only gets so far, it stops after printing "In" of the Serial.println("In Monitor") line. Why is that?
Don't count on the output to tell you where the error is. That serial print may have completed successfully and the message is buffered in an output stream waiting for a chance to be written to the serial port.
A much more likely cause of the crash is the line below the serial print.
Serial.println("In Monitor");
while(tPointer->getTurret().BottleCenterState==false)
tPointer is used and I don't see anywhere in the provided code it is assigned a valid, dereferencable pointer. Dereferencing an undefined pointer results in undefined behaviour, and in this case probably a crash. Even if it isn't the crash you are seeing, this is almost certainly wrong.
How to fix it?
From the code provided it doesn't look like tpointer needs to be a pointer at all.
Turret turret;
May be all you need. Allocating turret statically eliminates the possibility of pointer and memory management bugs and reduces the chance of leaks.
Otherwise,
Turret* tPointer = new Turret();
But this leaves you with the problem of how and when do you delete tPointer;.
Check that your string doesn't contain a NULL character. This will terminate the string abruptly.
I registred handler for simple QTextObjectInterface, that draws just 10x10 red rectangle.
When i used QTextEdit in normal QWidget app, it worked.
When i used QQuickTextEdit (TextEdit qml component) in Qt Quick app, it doesn't worked (nothing is drawed, but the rectangle in TextEdit is reserved, because when i change cursor position, i notice that there is something, but just empty space, nothing is drawed.
The QTextObjectInterface intrinsicSize method is called (that explains why i see there is empty space 10x10), but the drawObject method isn't.
I did some research and i found that actually the problem is probably here:
QQuickTextEdit.cpp from Qt 5.3.0 sources (line 1821)
QSGNode *QQuickTextEdit::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData) {
.
.
.
if (textFrame->firstPosition() > textFrame->lastPosition()
&& textFrame->frameFormat().position() != QTextFrameFormat::InFlow) {
updateNodeTransform(node, d->document->documentLayout()->frameBoundingRect(textFrame).topLeft());
const int pos = textFrame->firstPosition() - 1;
ProtectedLayoutAccessor *a = static_cast<ProtectedLayoutAccessor *>(d->document->documentLayout());
QTextCharFormat format = a->formatAccessor(pos);
QTextBlock block = textFrame->firstCursorPosition().block();
node->m_engine->setCurrentLine(block.layout()->lineForTextPosition(pos - block.position()));
node->m_engine->addTextObject(QPointF(0, 0), format, QQuickTextNodeEngine::Unselected, d->document,
pos, textFrame->frameFormat().position());
nodeStart = pos;
}
it never reaches the point where node->m_engine->addTextObject is called.
It is because
this part of if condition textFrame->firstPosition() > textFrame->lastPosition() is evaluated to false.
I tried std::cout the firstPostion and the lastPosition when i established the context and firstPosition is 0, lastPosition is 1.
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTextDocument>
#include <QQuickTextDocument>
#include <iostream>
#include <QTextCursor>
#include <QTextBlock>
#include <QPainter>
#include <QAbstractTextDocumentLayout>
#include <QTextCharFormat>
#include "qmlcomponentspace.h"
#include <QTextEdit>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QTextDocument * doc = engine.rootObjects().first()->findChild<QObject *>("editor")->property("textDocument").value<QQuickTextDocument *>()->textDocument();
QTextCursor cur(doc);
int objectType = QTextFormat::UserObject + 1000;
QmlComponentSpace * component = new QmlComponentSpace();
doc->documentLayout()->registerHandler(objectType, component);
QTextCharFormat fmt;
fmt.setObjectType(objectType);
fmt.setForeground(Qt::red);
fmt.setBackground(Qt::red);
cur.movePosition(QTextCursor::End);
cur.insertText(QString(QChar::ObjectReplacementCharacter), fmt);
std::cout << "FIRST:" << doc->rootFrame()->firstPosition() << std::endl;
std::cout << "END:" << doc->rootFrame()->lastPosition() << std::endl;
return app.exec();
}
What i am missing?
The documentation says at
http://doc.qt.io/qt-5/qquicktextdocument.html#details
Warning: The QTextDocument provided is used internally by Qt Quick elements to provide text manipulation primitives. You are not allowed to perform any modification of the internal state of the QTextDocument. If you do, the element in question may stop functioning or crash.
I am trying make my windows in GTKmm open in the centre of the screen and I can't find a working solution anywhere online. Any insights?
I have been trying everything I can think of with no results...
Here is my code:
#include "viewMenu.h"
#include <iostream>
#include <fstream>
#define MAX_BUF 10
viewMenu::viewMenu()
: frmMenu("cuTAES Review Menu"),
lblChooser("Select Course : "),
lblCourses(""),
lblView(" View "),
lblAppInfo(" Application Info "),
lblStuInfo(" Student Info "),
btnView(" View this Application "),
btnBack(" Back "),
frameTable(4,4,true)
{
set_title("cuTAES Review Menu ");
//set_border_width(50);
add(frameTable);
window.set_position(Gtk::WIN_POS_CENTER);
//frmMenu.add(lblChooser);
//frameTable.attach(frmMenu,0,1,0,0);
frameTable.attach(lblChooser, 1,2,0,1);
char text[MAX_BUF];
std::ifstream inFile("courses.txt", std::ios::in);
if(!inFile){
exit(1);
}
while(!inFile.eof()) {
inFile.getline(text,MAX_BUF);
cboCourse.append(text);
}
frameTable.attach(cboCourse, 2,3,0,1);
//set up my table
frameTable.attach(lblView, 0,1,0,1);
frameTable.attach(lblStuInfo, 0,1,1,2);
frameTable.attach(lblAppInfo, 2,3,1,2);
btnView.signal_clicked().connect(sigc::mem_fun(*this,&viewMenu::btnView_clicked));
frameTable.attach(btnView,3,4,0,1);
btnBack.signal_clicked().connect(sigc::mem_fun(*this, &viewMenu::btnBack_clicked));
frameTable.attach(btnBack,3,4,3,4);
show_all_children();
}
and my Header file:
#ifndef VIEWMENU_H
#define VIEWMENU_H
#include <gtkmm.h>
class viewMenu : public Gtk::Window
{
public:
viewMenu();
virtual ~viewMenu();
protected:
//Signal handlers:
void btnView_clicked();
void btnBack_clicked();
//Member widgets:
Gtk::Table frameTable;
Gtk::Label lblChooser,lblCourses, lblView, lblAppInfo, lblStuInfo, lblCourseDrop;
Gtk::Frame frmMenu;
Gtk::Button btnView, btnBack;
Gtk::ComboBoxText cboCourse;
Gtk::Window window;
};
#endif // MAINMENU_H
Your viewMenu class inherits from the Gtk::Window class. Simply use the set_position function to set the position of the window. Instead of:
window.set_position(Gtk::WIN_POS_CENTER);
Use:
set_position(Gtk::WIN_POS_CENTER);