How can we draw axis tick marks in both inward and outwards? I reimplemented QwtScaleDraw and overrode with drawTick but I don't know how to match the tick position and draw additional line using
QwtPainter::drawLine(painter,QPointF,QPointF)
I tried :
inside Plot::drawItems(QPainter *painter, const QRectF &rect, const QwtScaleMap map[axisCnt]) const
const QwtScaleMap ↦
for (j = 0; j < majTicks; j++)
{
y = map.transform(majTickList[j]);
QwtPainter::drawLine(painter, x, y, x + m_majTickLength, y);
}
but the axis margin is not matching with the corners of the out axis, small deviation is coming. I took a screenshot here :
my complete drawInward
void CustomScaleDraw::draw(QPainter *painter, const QPalette &palette) const
{
QwtScaleDraw::draw(painter, palette);
painter->save();
QPen pen = painter->pen();
pen.setColor(palette.color(QPalette::Foreground));
painter->setPen(pen);
int majLen = m_pPlotWidget->majorTickLength();
if (m_majTickStyle >= Both && majLen > 0){
QList<double> ticks = this->scaleDiv().ticks(QwtScaleDiv::MajorTick);
for (int i = 0; i < (int)ticks.count(); i++){
const double v = ticks[i];
if (this->scaleDiv().contains(v))
drawInwardTick(painter, v, majLen);
}
}
}
and
void CustomScaleDraw::drawInwardTick(QPainter *painter, double value, int len) const
{
int pw2 = qMin((int)painter->pen().width(), len) / 2;
QwtScaleMap scaleMap = this->scaleMap();
QPointF pos = this->pos();
int majLen = tickLength(QwtScaleDiv::MajorTick);
const int clw = m_pPlotWidget->lineWidth();
const int tval = scaleMap.transform(value);
bool draw = false;
if ( orientation() == Qt::Vertical ){
int low = (int)scaleMap.p2() + majLen;
int high = (int)scaleMap.p1() - majLen;
if ((tval > low && tval < high) ||
(tval > high && !m_pPlotWidget->axisEnabled (QwtPlot::xBottom) && !clw) ||
(tval < low && !m_pPlotWidget->axisEnabled(QwtPlot::xTop) && !clw)) draw = true;
} else {
int low = (int)scaleMap.p1() + majLen;
int high = (int)scaleMap.p2() - majLen;
if ((tval > low && tval < high) ||
(tval > high && !m_pPlotWidget->axisEnabled(QwtPlot::yRight) && !clw) ||
(tval < low && !m_pPlotWidget->axisEnabled(QwtPlot::yLeft) && !clw)) draw = true;
}
if (draw){
switch(alignment()){
case LeftScale:
{
QwtPainter::drawLine(painter, pos.x() + pw2, tval, pos.x() + len, tval);
break;
}
case RightScale:
{
QwtPainter::drawLine(painter, pos.x() - pw2, tval, pos.x() - len, tval);
break;
}
case BottomScale:
{
QwtPainter::drawLine(painter, tval, pos.y() - pw2, tval, pos.y() - len);
break;
}
case TopScale:
{
QwtPainter::drawLine(painter, tval, pos.y() + pw2, tval, pos.y() + len);
break;
}
}
}
// QwtPainter::setMetricsMap(metricsMap); // restore metrics map
}
my scale setup in QwtPlot.cpp
for (int i = 0; i < QwtPlot::axisCnt; i++)
{
QwtScaleWidget *scale = (QwtScaleWidget *) axisWidget(i);
if(scale)
{
scale->setMargin(0);
//the axis title color must be initialized...
QwtText title = scale->title();
title.setColor(Qt::black);
scale->setTitle(title);
//...same for axis color
QPalette pal = scale->palette();
pal.setColor(QPalette::Foreground, QColor(Qt::black));
scale->setPalette(pal);
CustomScaleDraw *sd = new CustomScaleDraw(this);
sd->setTickLength(QwtScaleDiv::MinorTick, m_minTickLength);
sd->setTickLength(QwtScaleDiv::MediumTick, m_minTickLength);
sd->setTickLength(QwtScaleDiv::MajorTick, m_majTickLength);
setAxisScaleDraw(i,sd);
}
}
plotLayout()->setAlignCanvasToScales( true );
m_minTickLength = 5;
m_majTickLength = 9;
You can achieve the effect of inward-pointed ticks by adding additional scale items to the plot, which have the opposite alignment property to what their position would normally require.
This requires way less code than a custom painter, and you don't have the alignment issues.
Output
Code
#include <qapplication.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_layout.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_scaleitem.h>
int main( int argc, char **argv )
{
// Enable high-DPI scaling with Qt 5.6+
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QApplication a( argc, argv );
QwtPlot plot;
plot.setTitle( "Plot Demo" );
plot.canvas()->setContentsMargins(0, 0, 0, 0);
plot.setStyleSheet("QwtPlot{ border: 0; }");
plot.canvas()->setStyleSheet("QwtPlotCanvas {border: none; margin: 1; background-color: white;}");
plot.plotLayout()->setCanvasMargin(0);
plot.enableAxis(QwtPlot::yLeft);
plot.enableAxis(QwtPlot::yRight);
plot.enableAxis(QwtPlot::xBottom);
plot.enableAxis(QwtPlot::xTop);
plot.setAxisScale( QwtPlot::yLeft, 0.0, 1000.0 );
plot.setAxisScale(QwtPlot::yRight, 0.0, 1000.0);
plot.setAxisScale(QwtPlot::xBottom, 0.0, 1000.0);
plot.setAxisScale(QwtPlot::xTop, 0.0, 1000.0);
plot.axisWidget(QwtPlot::yLeft)->setMargin(0);
plot.axisWidget(QwtPlot::yLeft)->setSpacing(0);
plot.axisWidget(QwtPlot::yRight)->setMargin(0);
plot.axisWidget(QwtPlot::xBottom)->setMargin(0);
plot.axisWidget(QwtPlot::xTop)->setMargin(0);
// create inward pointing ticks, and disable their labels
// notice the alignment is *opposite* to the position.
// in production code, don't hardcode the positions obviously.
QwtPlotScaleItem *yLeftScaleItem = new QwtPlotScaleItem(QwtScaleDraw::RightScale, 0);
yLeftScaleItem->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels, false);
yLeftScaleItem->attach(&plot);
QwtPlotScaleItem *yRightScaleItem = new QwtPlotScaleItem(QwtScaleDraw::LeftScale, 1000);
yRightScaleItem->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels, false);
yRightScaleItem->attach(&plot);
QwtPlotScaleItem *xBottomScaleItem = new QwtPlotScaleItem(QwtScaleDraw::TopScale, 0);
xBottomScaleItem->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels, false);
xBottomScaleItem->attach(&plot);
QwtPlotScaleItem *xTopScaleItem = new QwtPlotScaleItem(QwtScaleDraw::BottomScale, 1000);
xTopScaleItem->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels, false);
xTopScaleItem->attach(&plot);
plot.updateCanvasMargins();
plot.resize( 500, 400 );
plot.show();
return a.exec();
}
Related
I generate a matrix about my game map by random Prim. Then I draw the map by menuItem in the cocos2d-x 4.0. I want to control a sprite to move by key "asdw". But I cannot control the sprite to cross the bamboo walls. Sometimes the sprite is surrounded by bamboo walls.
How can I check the wall? How can I ensure a path that the sprite can move to the destination?
This is my result image
cross the wall
sprite is surrounded
My code is below.
MazeScene.h
#pragma once
#ifndef __Maze_SCENE_H__
#define __Maze_SCENE_H__
#endif // __SECOND_SCENE_H__
#include "cocos2d.h"
#include<vector>
#include <time.h>
#define m 18 //row
#define n 10 //columns
#define down 1
#define right 2
#define left 4
#define up 8
#define WALL -1
#define NOTHING 2
USING_NS_CC;
class MazeScene : public cocos2d::Layer {
public:
Sprite* sprite;
int backgroundAudioID;
static cocos2d::Scene* createScene();
virtual bool init();
void initMap();
void onReset();
void changeScene(float dt);
void update(float dt);
void GameOver();
void Success();
void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event);
void FindBlock();
CREATE_FUNC(MazeScene);
private:
struct block {
int row, column, direction;
block(int _row, int _column, int _direction) {
row = _row;
column = _column;
direction = _direction;
}
};
struct point {
int x;
int y;
}start, end;
std::vector<block> myblock;
int x_num = 1, y_num = 1;//Miner location
int G[100][100];
Sprite* _player;
Sprite* _door;
Menu* mazeMenu;
int life = 180;
int i = 0;
};
MazeScene.cpp
#include "MazeScene.h"
#include <AudioEngine.h>
#include <MenuScene.h>
USING_NS_CC;
using namespace std;
#define UserDefault UserDefault::getInstance()
cocos2d::Scene* MazeScene::createScene()
{
auto scene = Scene::create();
auto layer = MazeScene::create();
scene->addChild(layer);
return scene;
}
bool MazeScene::init()
{
if (!Layer::init())
{
return false;
}
AudioEngine::play2d("music/leisure.mp3",true);
// special effect
auto particleSystem = ParticleSystemQuad::create("image/mist.plist");
particleSystem->setPosition(1500, 500);
this->addChild(particleSystem, 1);
auto particleSystem1 = ParticleSystemQuad::create("image/mist2.plist");
particleSystem1->setPosition(-500, 400);
this->addChild(particleSystem1, 1);
this->schedule(CC_SCHEDULE_SELECTOR(MazeScene::update), 1.0);
initMap();
return true;
}
void MazeScene::initMap() {
//Put all the map as a wall
memset(G, WALL, sizeof(G));
//Define the starting point
G[1][1] = NOTHING;
start.x = start.y = 1;
srand((unsigned)time(NULL));//Random number seed
FindBlock();
//The first step is to press into the two walls (to the right of the starting point and
below the starting point) to enter the loop
while (myblock.size()) {
int BlockSize = myblock.size();
//Randomly select a wall (generate a random number between 0 and BlockSize-1, which
is also the subscript of the wall in the vector)
int randnum = rand() % BlockSize;
block SelectBlock = myblock[randnum];
x_num = SelectBlock.row;//Miners come to our "wall of choice" here
y_num = SelectBlock.column;
//Follow-up operations based on the direction of the currently selected wall
//At this time, the three areas of the wall and target block selected as the
starting point are on the same straight line.
//We let the miners move on from the "wall of choice" to the "target block"
//Miners have the ability to penetrate walls :)
switch (SelectBlock.direction) {
case down: {
x_num++;
break;
}
case right: {
y_num++;
break;
}
case left: {
y_num--;
break;
}
case up: {
x_num--;
break;
}
}
//If the target block is a wall
if (G[x_num][y_num] == WALL) {
//Break through the wall and target block
G[SelectBlock.row][SelectBlock.column] = G[x_num][y_num] = NOTHING;
//Find again the wall adjacent to the current location of the miner
FindBlock();
}
else {//If not? It means that our miners have dug into an empty path and just take a
rest.
//relax
}
//Delete this wall (delete the unusable wall, for those who have already been
constructed, there is no need to construct it, but also to ensure that we can jump
out of the loop)
myblock.erase(myblock.begin() + randnum);
}
Vector<MenuItem*> spriteBtns;
std::string imgName;
for (int i = 0; i <= m + 1; i++) {
for (int j = 0; j <= n + 1; j++) {
if (i == start.x && j == start.y) {
imgName = "general.png";
}else if(G[i][j] == NOTHING) {
imgName = "nud.png";
}
else if(i == m + 1 && j == n + 1) {
imgName = "image/evidence.png";
}
else {
imgName = "bamboo.png";
}
auto btnSprite = MenuItemImage::create(imgName,imgName);
Size cts = btnSprite->getContentSize();
btnSprite->setPosition(i*cts.width,j*cts.height);
spriteBtns.pushBack(btnSprite);
}
}
mazeMenu = Menu::createWithArray(spriteBtns);
addChild(mazeMenu);
mazeMenu->setPosition(30,80);
_player = Sprite::create("general.png");
_player->setPosition(90, 130);
this->addChild(_player);
_door = Sprite::create("image/evidence.png");
_door->setPosition(900,630);
this->addChild(_door);
// Keyboard event monitoring
auto keyListener = EventListenerKeyboard::create();
keyListener->onKeyPressed = CC_CALLBACK_2(MazeScene::onKeyPressed, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this);
}
void MazeScene::onReset() {
initMap();
}
void MazeScene::changeScene(float dt) {
Director::getInstance()->replaceScene(TransitionSlideInT::create(3.0f,
MenuScene::createScene()));
}
void MazeScene::update(float dt) {
life--;
if (_player->getBoundingBox().intersectsRect(_door->getBoundingBox())) {
initMap();
i++;
}
if (life<=0) {
GameOver();
this->scheduleOnce(CC_SCHEDULE_SELECTOR(MazeScene::changeScene), 5.0);
}
if (i == 3 && life > 0) {
UserDefault->setIntegerForKey("step4", 4);
UserDefault->flush();
Success();
this->scheduleOnce(CC_SCHEDULE_SELECTOR(MazeScene::changeScene), 5.0);
}
}
void MazeScene::GameOver() {
this->unscheduleAllCallbacks();
AudioEngine::play2d("music/over.mp3", false, 0.3);
AudioEngine::stopAll();
auto sprite = Sprite::create("image/over2.png");
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 +
origin.y));
this->addChild(sprite, 10);
}
void MazeScene::Success() {
this->unscheduleAllCallbacks();
AudioEngine::play2d("music/victory.mp3", false, 0.3);
AudioEngine::stopAll();
auto sprite = Sprite::create("image/success2.png");
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 +
origin.y));
this->addChild(sprite, 10);
}
void MazeScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) {
log("Key with keycode %d pressed", keyCode);
switch (keyCode) {
case EventKeyboard::KeyCode::KEY_D:
{
auto moveBy = MoveBy::create(0.5f, Vec2(25, 0));
_player->runAction(Sequence::create(moveBy, nullptr));
break;
}
case EventKeyboard::KeyCode::KEY_A:
{
auto moveBy = MoveBy::create(0.5f, Vec2(-25, 0));
_player->runAction(Sequence::create(moveBy, nullptr));
break;
}
case EventKeyboard::KeyCode::KEY_W:
{
auto moveBy = MoveBy::create(0.5f, Vec2(0, 25));
_player->runAction(Sequence::create(moveBy, nullptr));
break;
}
case EventKeyboard::KeyCode::KEY_S:
{
auto moveBy = MoveBy::create(0.5f, Vec2(0, -25));
_player->runAction(Sequence::create(moveBy, nullptr));
break;
}
default:
break;
}
}
void MazeScene::FindBlock() {
//find Find nearby walls
if (x_num + 1 <= m && G[x_num + 1][y_num] == WALL) {//down
myblock.push_back(block(x_num + 1, y_num, down));
}
if (y_num + 1 <= n && G[x_num][y_num + 1] == WALL) {//right
myblock.push_back(block(x_num, y_num + 1, right));
}
if (x_num - 1 >= 1 && G[x_num - 1][y_num] == WALL) {//up
myblock.push_back(block(x_num - 1, y_num, up));
}
if (y_num - 1 >= 1 && G[x_num][y_num - 1] == WALL) {//left
myblock.push_back(block(x_num, y_num - 1, left));
}
}
When I ran the following code, I expected a result where pressing one of three buttons would each trigger a matching state that would render a matching image adjacent to those buttons, allowing me to rotate through fresh images at leisure with each successive button press.
Instead of that happy scenario, any initial button press skips right to the last image...or does nothing...or renders a blank white screen depending on how I fiddle with my placement of the initial state declaration and whether I place the state switch within or without the event handling switch. The whole system breaks and stops dead in its tracks. What is going on, and how do I fix it?
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "cleanup.h"
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Rect* clip = NULL;
const int LONGBUTTON_HEIGHT = 128;
const int LONGBUTTON_WIDTH = 256;
int Screen_Width = 640;
int Screen_Height = 480;
int mouse_x;
int mouse_y;
int alphabutton_x = 0;
int alphabutton_y = 5;
int alphabutton_h = Screen_Width / 10;
int alphabutton_w = Screen_Width / 5;
int betabutton_x = 0;
int betabutton_y = 0.5 * (Screen_Width / 5) + 5;
int betabutton_h = Screen_Width / 10;
int betabutton_w = Screen_Width / 5;
int gammabutton_x = 0;
int gammabutton_y = 1 * (Screen_Width / 5) + 5;
int gammabutton_h = Screen_Width / 10;
int gammabutton_w = Screen_Width / 5;
int alpha_x = Screen_Width / 5;
int alpha_y = 0;
int alpha_h = Screen_Height;
int alpha_w = (4* Screen_Width)/5;
int beta_x = Screen_Width / 5;
int beta_y = 0;
int beta_h = Screen_Height;
int beta_w = (4* Screen_Width)/5;
int gamma_x = Screen_Width / 5;
int gamma_y = 0;
int gamma_h = Screen_Height;
int gamma_w = (4* Screen_Width)/5;
enum alphaButtonSprite {ALPHA_DEFAULT, ALPHA_HOVER, ALPHA_INACTIVE, ALPHA_PRESSED, ALPHA_TOTAL};
enum betaButtonSprite {BETA_DEFAULT, BETA_HOVER, BETA_INACTIVE, BETA_PRESSED, BETA_TOTAL};
enum gammaButtonSprite {GAMMA_DEFAULT, GAMMA_HOVER, GAMMA_INACTIVE, GAMMA_PRESSED, GAMMA_TOTAL};
enum State {ALPHA_STATE, BETA_STATE, GAMMA_STATE};
State state;
SDL_Texture* loadTexture(const std::string& file, SDL_Renderer* renderer)
{
SDL_Texture* texture = IMG_LoadTexture(renderer, file.c_str());
return texture;
}
void renderTexture(SDL_Texture* texture, SDL_Renderer* renderer, SDL_Rect dest,
SDL_Rect* clip = nullptr)
{
SDL_RenderCopy(renderer, texture, clip, &dest);
}
void renderTexture(SDL_Texture* texture, SDL_Renderer* renderer, int x, int y, int h, int w,
SDL_Rect* clip = nullptr)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.h = h;
dest.w = w;
renderTexture(texture, renderer, dest, clip);
}
// Main Function
int main(int, char**)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("New Window", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, Screen_Width, Screen_Height, SDL_WINDOW_RESIZABLE);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Texture* longbutton_image = loadTexture("longbuttonSpriteSheet.png", renderer);
SDL_Texture* alpha_image = loadTexture("alphaImage.png", renderer);
SDL_Texture* beta_image = loadTexture("betaImage.png", renderer);
SDL_Texture* gamma_image = loadTexture("gammaImage.png", renderer);
state = BETA_STATE;
SDL_Rect alpha_clips[alphaButtonSprite::ALPHA_TOTAL];
for (int i = 0; i < alphaButtonSprite::ALPHA_TOTAL; i++)
{
alpha_clips[i].x = i * LONGBUTTON_WIDTH;
alpha_clips[i].y = 0;
alpha_clips[i].w = LONGBUTTON_WIDTH;
alpha_clips[i].h = LONGBUTTON_HEIGHT;
}
int usealpha_Clip = ALPHA_DEFAULT;
SDL_Rect beta_clips[betaButtonSprite::BETA_TOTAL];
for (int i = 0; i < betaButtonSprite::BETA_TOTAL; i++)
{
beta_clips[i].x = i * LONGBUTTON_WIDTH;
beta_clips[i].y = 4 * LONGBUTTON_HEIGHT;
beta_clips[i].w = LONGBUTTON_WIDTH;
beta_clips[i].h = LONGBUTTON_HEIGHT;
}
int usebeta_Clip = BETA_DEFAULT;
SDL_Rect gamma_clips[gammaButtonSprite::GAMMA_TOTAL];
for (int i = 0; i < gammaButtonSprite::GAMMA_TOTAL; i++)
{
gamma_clips[i].x = i * LONGBUTTON_WIDTH;
gamma_clips[i].y = 5 * LONGBUTTON_HEIGHT;
gamma_clips[i].w = LONGBUTTON_WIDTH;
gamma_clips[i].h = LONGBUTTON_HEIGHT;
}
int usegamma_Clip = GAMMA_DEFAULT;
SDL_Event e;
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&e))
{
switch (e.type)
{
case SDL_MOUSEBUTTONDOWN:
mouse_x = e.button.x;
mouse_y = e.button.y;
if ((mouse_x <= (alphabutton_x + alphabutton_w)) && (mouse_x > alphabutton_x) &&
(mouse_y <= (alphabutton_y + alphabutton_h)) && (mouse_y > alphabutton_y))
usealpha_Clip = ALPHA_PRESSED;
state = ALPHA_STATE;
if ((mouse_x <= (betabutton_x + betabutton_w)) && (mouse_x > betabutton_x) &&
(mouse_y <= (betabutton_y + betabutton_h)) && (mouse_y > betabutton_y))
usebeta_Clip = BETA_PRESSED;
state = BETA_STATE;
if ((mouse_x <= (gammabutton_x + gammabutton_w)) && (mouse_x > gammabutton_x) &&
(mouse_y <= (gammabutton_y + gammabutton_h)) && (mouse_y > gammabutton_y))
usegamma_Clip = GAMMA_PRESSED;
state = GAMMA_STATE;
break;
}
}
switch (state)
{
case ALPHA_STATE:
SDL_RenderClear(renderer);
renderTexture(longbutton_image, renderer, alphabutton_x, alphabutton_y, alphabutton_h, alphabutton_w, &alpha_clips[usealpha_Clip]);
renderTexture(longbutton_image, renderer, betabutton_x, betabutton_y, betabutton_h, betabutton_w, &beta_clips[usebeta_Clip]);
renderTexture(longbutton_image, renderer, gammabutton_x, gammabutton_y, gammabutton_h, gammabutton_w, &gamma_clips[usegamma_Clip]);
renderTexture(alpha_image, renderer, alpha_x, alpha_y, alpha_h, alpha_w, nullptr);
SDL_RenderPresent(renderer);
break;
case BETA_STATE:
SDL_RenderClear(renderer);
renderTexture(longbutton_image, renderer, alphabutton_x, alphabutton_y, alphabutton_h, alphabutton_w, &alpha_clips[usealpha_Clip]);
renderTexture(longbutton_image, renderer, betabutton_x, betabutton_y, betabutton_h, betabutton_w, &beta_clips[usebeta_Clip]);
renderTexture(longbutton_image, renderer, gammabutton_x, gammabutton_y, gammabutton_h, gammabutton_w, &gamma_clips[usegamma_Clip]);
renderTexture(beta_image, renderer, beta_x, beta_y, beta_h, beta_w, nullptr);
SDL_RenderPresent(renderer);
break;
case GAMMA_STATE:
SDL_RenderClear(renderer);
renderTexture(longbutton_image, renderer, alphabutton_x, alphabutton_y, alphabutton_h, alphabutton_w, &alpha_clips[usealpha_Clip]);
renderTexture(longbutton_image, renderer, betabutton_x, betabutton_y, betabutton_h, betabutton_w, &beta_clips[usebeta_Clip]);
renderTexture(longbutton_image, renderer, gammabutton_x, gammabutton_y, gammabutton_h, gammabutton_w, &gamma_clips[usegamma_Clip]);
renderTexture(gamma_image, renderer, gamma_x, gamma_y, gamma_h, gamma_w, nullptr);
SDL_RenderPresent(renderer);
break;
}
}
//Destroy the various items
cleanup(longbutton_image, alpha_image, beta_image, gamma_image, renderer, window);
IMG_Quit();
SDL_Quit();
return 0;
}
Found the answer to this one. In the mousebuttondown event switch cases, just put the state code before the useclip code. Why it makes a difference, I don't know, but it works. Cough...Also, doing it in the mousebuttonup switch cases instead of mousebuttondown works even better.
So I was determined to get collision working myself from online resources without asking for help & I successfully followed a tutorial on lazyfoo to get collision working but quickly realised other problems.
So I started trying to get my collision working on my player (just a rect with controls atm) but couldn't actually access the rect inside the player class, I had to initialize it in main & use that for collision which meant transferring my collision function to main (not exactly perfect).
My question: So my collision is limited to two rects which I dedicated to the player & a single asteroid but my idea was to create multiple asteroids coming from the top of the screen, how would I go about altering the below code to accept a vector of "asteroids". Any advice about altering my code to be better object oriented etc is more then welcome but my main problem is collision. Please read collision carefully before posting, the specific X and Y params from the .cpp provides are hardcoded there.
Below Code order:
My current way of defining both player & asteroid for collision
Collision function
Asteroid.cpp & Player.cpp
My new way of loading asteroids - This is what I need collision to work with
Player aPlayer(200, 50, 50, 50);
Asteroid oneAsteroid(200, -50, 50, 50);
bool check_collision(SDL_Rect aPlayer, SDL_Rect oneAsteroid) {
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = aPlayer.X;
rightA = aPlayer.X + aPlayer.width;
topA = aPlayer.Y;
bottomA = aPlayer.Y + aPlayer.height;
//Calculate the sides of rect B
leftB = oneAsteroid.X;
rightB = oneAsteroid.X + oneAsteroid.width;
topB = oneAsteroid.Y;
bottomB = oneAsteroid.Y + oneAsteroid.height;
if (bottomA <= topB)
{
return false;
}
if (topA >= bottomB)
{
return false;
}
if (rightA <= leftB)
{
return false;
}
if (leftA >= rightB)
{
return false;
}
//If none of the sides from A are outside B
return true;
}
#include "asteroids.h"
Asteroid::Asteroid()
{
}
Asteroid::~Asteroid()
{
}
Asteroid::Asteroid(int x, int y, int w, int h)
{
X = x; Y = y; width = w; height = h;
//SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Square Constructed with Param(&p)", this);
}
void Asteroid::Render(SDL_Renderer * aRenderer)
{
printf("");
SDL_Rect* aAsteroid = new SDL_Rect();
aAsteroid->x = X; aAsteroid->y = Y; aAsteroid->w = width; aAsteroid->h = height;
SDL_SetRenderDrawColor(aRenderer, 0, 0, 0, 255);
SDL_RenderFillRect(aRenderer, aAsteroid);
}
void Asteroid::Init()
{
velocity.X = 0;
velocity.Y = 5;
}
void Asteroid::Update()
{
Y = Y + velocity.Y;
// printf("%d \n", Astero);
if (Y > 650) {
Y = -50;
}
}
#include "Player.h"
#include "asteroids.h"
using namespace std;
Player::Player()
{
}
Player::~Player()
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Player Destroyed (&p)", this);
}
Player::Player(int x, int y, int w, int h)
{
X = x; Y = y; width = w; height = h;
//SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Square Constructed with Param(&p)", this);
}
void Player::Init()
{
velocity.X = 10;
velocity.Y = 10;
}
void Player::Update()
{
Y = Y + floorStart;
if (X < 0) {
X = 0;
}
if (KEY_RIGHT == true) {
X++;
}
if (X > SCREEN_WIDTH - 50) {
X = SCREEN_WIDTH - 50;
}
if (Y > SCREEN_HEIGHT - 100) {
Y = SCREEN_HEIGHT - 100;
}
if (KEY_LEFT == true) {
X--;
}
}
void Player::Input(SDL_Event event)
{
if (event.type == SDL_KEYDOWN && event.key.repeat == NULL)
{
if (event.key.keysym.sym == SDLK_LEFT) {
KEY_LEFT = true;
}
else {
KEY_LEFT = false;
}
if (event.key.keysym.sym == SDLK_RIGHT) {
KEY_RIGHT = true;
}
else {
KEY_RIGHT = false;
}
}
if (event.type == SDL_KEYUP && event.key.repeat == NULL) {
if (event.key.keysym.sym == SDLK_LEFT) {
KEY_LEFT = false;
}
if (event.key.keysym.sym == SDLK_RIGHT) {
KEY_RIGHT = false;
}
}
}
void Player::Render(SDL_Renderer * aRenderer)
{
SDL_Rect* thePlayer = new SDL_Rect;
thePlayer->x = X; thePlayer->y = Y; thePlayer->w = width; thePlayer->h = height;
SDL_SetRenderDrawColor(aRenderer, 0, 0, 0, 255);
SDL_RenderFillRect(aRenderer, thePlayer);
//SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Rendering (&p)", this);
}
What I need collision to work with, a vector of asteroids:
std::vector<Asteroid*> asteroidList;
asteroidList.push_back(new Asteroid(150, 350, 50, 50));
asteroidList.push_back(new Asteroid(70, 120, 125, 125));
The function to check collision in main.cpp
if (check_collision(aPlayer.thePlayer, oneAsteroid.aAsteroid)) {
printf("#######################");
}
And lastly, how would I go about rendering the array of asteroids?
I started writing a little game but something with the image is not working. They're working fine at the beginning but after some moments there become black and then they disappear.
Don't be worried about the long code the relevant things with the images happen mostly in the following methods: build_mode_draw() and play_mode_draw().
I tested the program with a blue cube instead of a image and it worked fine
Probably I don't really understand how the image gets loaded
#include <SDL2/SDL.h>
#define WindowWidth 1500
#define WindowHight 800
#define ArrayGrosseBuildMode 100
#define StartFensterBlockmenge 10 // in Plockgröße bezüglich der kleineren Achse
bool end = false;
bool programm_part_run = true;
unsigned int play_mode_speed = 0;
unsigned int counter;
unsigned int counter_2;
unsigned int counter_3;
unsigned int blocksize;
unsigned int blocks_fit_in_X;
unsigned int blocks_fit_in_Y;
unsigned int play_mode_blockamount;
//unsigned int blockamount = 0;
bool build_mode_block_pos[ArrayGrosseBuildMode][ArrayGrosseBuildMode];
unsigned int play_mode_block_pos_X[WindowWidth]; // Fächer beschreiben
unsigned int play_mode_block_pos_Y[WindowHight]; // ||
//mouse variables
unsigned short int pressed_mouse_button = 0; // 0 = no , 1 = left , mouse Button pressed
unsigned int MouseX;
unsigned int MouseY;
//keyboard variables
//set window
SDL_Window* window = NULL;
//set renderer
SDL_Renderer* renderer = NULL;
//set event
SDL_Event event;
void input()
{
SDL_PollEvent(&event);
// reset variables
pressed_mouse_button = 0; // set to no mouse button pressed
switch(event.type)
{
case SDL_QUIT:
end = true;
programm_part_run = false;
break;
case SDL_MOUSEMOTION:
MouseX = event.motion.x;
MouseY = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
switch(event.button.button)
{
case SDL_BUTTON_LEFT:
pressed_mouse_button = 1;
break;
}
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_SPACE:
programm_part_run = false;
break;
}
}
}
void put_build_mode_grid_in_renderer()
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
for(counter = 0; counter <= blocks_fit_in_Y; counter = counter + 1)
{
SDL_RenderDrawLine(renderer,0,counter * blocksize,blocks_fit_in_X*blocksize,counter * blocksize);
}
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
SDL_RenderDrawLine(renderer,counter * blocksize,0,counter * blocksize,blocks_fit_in_Y*blocksize);
}
}
void build_mode_draw()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
SDL_RenderClear(renderer);
put_build_mode_grid_in_renderer();
SDL_Surface * image = SDL_LoadBMP("stealcube.bmp");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,image);
SDL_FreeSurface(image);
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
for(counter_2 = 0; counter_2 <= blocks_fit_in_Y; counter_2 = counter_2 + 1)
{
if(build_mode_block_pos[counter][counter_2] == true)
{
SDL_Rect dstrect = { counter * blocksize, counter_2 * blocksize, blocksize, blocksize};
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
}
}
}
SDL_RenderPresent(renderer);
}
void build_mode()
{
while(programm_part_run)
{
input();
if(pressed_mouse_button == 1)
{
build_mode_block_pos[MouseX/blocksize][MouseY/blocksize] = true;
}
build_mode_draw();
}
}
void play_mode_draw()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
SDL_RenderClear(renderer);
SDL_Surface * image = SDL_LoadBMP("stealcube.bmp");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,image);
SDL_FreeSurface(image);
for(counter = 0; counter < play_mode_blockamount; counter = counter + 1)
{
SDL_Rect dstrect = { play_mode_block_pos_X[counter], play_mode_block_pos_Y[counter], blocksize, blocksize};
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
}
SDL_RenderPresent(renderer);
}
void play_mode()
{
counter_3 = 0;
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
for(counter_2 = 0; counter_2 <= blocks_fit_in_Y; counter_2 = counter_2 + 1)
{
if(build_mode_block_pos[counter][counter_2] == true)
{
play_mode_block_pos_X[counter_3] = counter*blocksize;
play_mode_block_pos_Y[counter_3] = counter_2*blocksize;
counter_3 = counter_3 + 1;
}
}
}
play_mode_blockamount = counter_3;
while(programm_part_run)
{
for(counter = 0; counter < play_mode_speed; counter = counter + 1)
{
input();
SDL_Delay(1);
}
for(counter = 0; counter <= play_mode_blockamount; counter = counter + 1)
{
if(play_mode_block_pos_Y[counter] < blocks_fit_in_Y * blocksize - blocksize)
{
play_mode_block_pos_Y[counter] = play_mode_block_pos_Y[counter] + 1;
}
}
play_mode_draw();
}
}
int main (int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow
(
"Test Fenster :)", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WindowWidth,
WindowHight,
SDL_WINDOW_SHOWN
);
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
//setup
if(WindowWidth < WindowHight)
{
blocksize = WindowWidth/StartFensterBlockmenge;
}
else
{
blocksize = WindowHight/StartFensterBlockmenge;
}
blocks_fit_in_X = WindowWidth/blocksize;
blocks_fit_in_Y = WindowHight/blocksize;
while(!end)
{
programm_part_run = true;
build_mode();
programm_part_run = true;
play_mode();
}
}
I am a novice C++ programmer, and I have been experimenting with the SDL libraries for 2d graphics. I have been messing around with try to create some simple GUI controls; however, my button control is having a weird issue that I have been unable to resolve.
Whenever I launch the program in debug mode Visual Studio immediately gives me an error window saying
"Windows has triggered a breakpoint in Default_SDL_Setup.exe.
This may be due to a corruption of the heap, which indicates a bug in
Default_SDL_Setup.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while
Default_SDL_Setup.exe has focus.
The output window may have more diagnostic information."
and pops up a file called crtexe.c in the code editor window.
I have no idea what is wrong or what is going on.
Here is my button header:
#ifndef BUTTON_T
#define BUTTON_T
#include <vector>
#include <string>
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDLFunctions.h"
#endif
class button
{
private:
//refreshes the button
void refresh_image();
bool refresh;
//surface to hold the created text
SDL_Surface *message;
//color of text
SDL_Color tC;
//the different box types
struct boxColor
{
unsigned int r;
unsigned int g;
unsigned int b;
};
boxColor boxes[4];
SDL_Rect rects[4];
//font of text
TTF_Font *f;
/*button's state
* States are:
* 0 - mouseout
* 1 - mouseover
* 2 - mousedown
* 3 - mouseup
*/
int state;
public:
//autosize based off of text?
bool autoSize;
//size of text
int textSize;
//x,y,w,and h for the box
int x, y, w, h;
//text of the label
std::string text;
button(int xpos, int ypos, int width, int height);
~button();
//handles all events for the button
void handleEvents(SDL_Event *event);
//sets text's font style off of a string location, color expects a 3 dimensional array
void setFont(std::string fontFileLocation);
void setFont(int color[3]);
void setFont(std::string fontFileLocation, int color[3]);
//gets the text color as a vector
std::vector<int> textColor();
//returns the color of the specified box
std::vector<int> boxColor(int boxnum);
//sets the color of the specified box, expects a 3 dimensional array
void setBoxColor(int boxnum, int color[3]);
void render(SDL_Surface *screen);
};
Here is the button code:
#include "button.h"
using namespace std;
void button::refresh_image()
{
if (refresh)
{
if (f != NULL)
{
message = TTF_RenderText_Solid(f, text.c_str(), tC);
if ((message != NULL) && autoSize)
{
if (message->w >= w)
{
w = (message->w + 10);
}
if (message->h >= h)
{
h = (message->h + 10);
}
}
}
for (int i = 0; i != 4; ++i)
{
rects[i].x = x;
rects[i].y = y;
rects[i].w = w;
rects[i].h = h;
}
refresh = false;
}
}
button::button(int xpos, int ypos, int width, int height)
{
string defaultFontLocation = DEFAULT_FONT;
//set pos
x = xpos;
y = ypos;
w = width;
h = height;
//set text stuff
text = "";
textSize = DEFAULT_FONT_SIZE;
tC.r = 0;
tC.g = 0;
tC.b = 0;
f = TTF_OpenFont(defaultFontLocation.c_str(), textSize);
message = NULL;
//create box types
state = 0;
for (int i = 0; i != 4; ++i)
{
boxes[i].r = 255;
boxes[i].g = 255;
boxes[i].b = 255;
rects[i].x = x;
rects[i].y = y;
rects[i].w = w;
rects[i].h = h;
}
autoSize = true;
refresh = true;
refresh_image();
}
button::~button()
{
if (f != NULL)
{
TTF_CloseFont(f);
}
if (message != NULL)
{
SDL_FreeSurface(message);
}
}
void button::setFont(string fontFileLocation)
{
if (f != NULL)
{
TTF_CloseFont(f);
}
f = TTF_OpenFont(fontFileLocation.c_str(), textSize);
refresh_image();
}
void button::setFont(int color[3])
{
tC.r = color[0];
tC.g = color[1];
tC.b = color[2];
refresh_image();
}
void button::setFont(string fontFileLocation, int color[3])
{
if (f != NULL)
{
TTF_CloseFont(f);
}
f = TTF_OpenFont(fontFileLocation.c_str(), textSize);
tC.r = color[0];
tC.g = color[1];
tC.b = color[2];
refresh_image();
}
vector<int> button::textColor()
{
vector<int> colors;
colors.resize(3);
colors[0] = tC.r;
colors[1] = tC.g;
colors[2] = tC.b;
return colors;
}
vector<int> button::boxColor(int boxnum)
{
vector<int> colors;
colors.resize(3);
if ((boxnum <= 3) && (boxnum >= 0))
{
colors[0] = boxes[boxnum].r;
colors[1] = boxes[boxnum].g;
colors[2] = boxes[boxnum].b;
} else {
colors[0] = -1;
colors[1] = -1;
colors[2] = -1;
}
return colors;
}
void button::setBoxColor(int boxnum, int color[3])
{
if ((boxnum <= 3) && (boxnum >= 0))
{
boxes[boxnum].r = color[0];
boxes[boxnum].g = color[1];
boxes[boxnum].b = color[2];
//refresh_image();
}
}
void button::render(SDL_Surface *screen)
{
refresh_image();
SDL_FillRect(screen, &rects[state], SDL_MapRGB(screen->format, static_cast<Uint8>(boxes[state].r), static_cast<Uint8>(boxes[state].g), static_cast<Uint8>(boxes[state].b)));
if (message != NULL)
{
apply_surface((((x + w) - message->w) / 2), (((y + h) - message->h) / 2), message, screen);
}
refresh = true;
}
void button::handleEvents(SDL_Event *event)
{
//the mouse offsets
int mx, my;
//if mouse moved
if (event->type == SDL_MOUSEMOTION)
{
//get the mouse offsets
mx = event->motion.x;
my = event->motion.y;
//if the mouse is over the button
if ((mx > x) && (mx < (x + w)) && (my > y) && (my < (y + h)))
{
//set the button sprite
state = 1;
} else {
//set the button sprite
state = 0;
}
}
//if a mouse button was pressed
if (event->type == SDL_MOUSEBUTTONDOWN)
{
//if it was the left mouse button
if (event->button.button == SDL_BUTTON_LEFT)
{
//get the mouse offsets
mx = event->motion.x;
my = event->motion.y;
//if the mouse is over the button
if ((mx > x) && (mx < (x + w)) && (my > y) && (my < (y + h)))
{
//set the button sprite
state = 2;
}
}
}
//if a mouse button was released
if (event->type == SDL_MOUSEBUTTONUP)
{
//if it was the left mouse button
if (event->button.button == SDL_BUTTON_LEFT)
{
//get the mouse offsets
mx = event->motion.x;
my = event->motion.y;
//if the mouse is over the button
if ((mx > x) && (mx < (x + w)) && (my > y) && (my < (y + h)))
{
//set the button sprite
state = 3;
}
}
}
}
and here is the code I have been calling to create the button in my main() function:
int color1[3] = {105,240,81};
int color2[3] = {230,188,62};
button testbutton(360, 130, 50, 30);
testbutton.text = "TEST";
testbutton.setBoxColor(1, color1);
testbutton.setBoxColor(2, color2);
delete[] &color1;
delete[] &color2;
The problem is most probably in these two lines:
delete[] &color1;
delete[] &color2;
color1 and color2are automatic variables, and calling delete on them invokes undefined behavior. You should only delete variables you previously allocated with new.