I have an SDI mfc application in which a bitmap image loaded, i want to rotate that bitmap 90Deg, 180Deg & 270Deg.
Am able to rotate it 180 degree. But am stuck at 90 and 270 deg.
Here is the code.
void CBmpView::OnRotate180()
{
BYTE ptempBit ;
CBmpViewerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int nLenght = (pDoc->dwBitsSize);
if(pBitsView != NULL){
DeleteObject(pBitsView);
}
if(pDoc->m_bRotationFlag == FALSE){
pDoc->m_bmpHandle = CreateDIBSection(NULL, pDoc->m_bitmapinfo,DIB_RGB_COLORS,
(void **)&pBitsView, NULL, 0);
CopyMemory(pBitsView,pDoc->m_pBits,pDoc->dwBitsSize);
for(int i=0; i < ((nLenght-1)) ; i++ ){
ptempBit = pBitsView[i];
pBitsView[i] = pBitsView[nLenght - 1];
pBitsView[nLenght - 1] = ptempBit;
nLenght --;
}
pDoc->m_bRotationFlag = TRUE;
Invalidate();
}else if(pDoc->m_bRotationFlag == TRUE){
pDoc->m_bmpHandle = CreateDIBSection(NULL, pDoc->m_bitmapinfo,DIB_RGB_COLORS,
(void **)&pBitsView, NULL, 0);
CopyMemory(pBitsView,pDoc->m_pBits,pDoc->dwBitsSize);
pDoc->m_bRotationFlag = FALSE;
Invalidate();
}
}
Please tell me if any other method can be used.
Thanks in advance
Just some pseudo code.
Let us assume an grid with iMaxRows and iMaxCols with rows and columns.
Should be:
// Sourcegrid is always
Pixels oldGrid[iMaxRows][iMaxCols]
+
// 90°
Pixels newGrid[iMaxCols][iMaxRows];
for (int iRow=0; iRow<iMaxRows; ++iRow)
{
for (int iCol=0; iCol<iMaxCols; ++iCol)
newGrid[iCol][iMaxRow-iRow-1] = oldGrid[iRow][iCol];
}
+
// 180°
Pixels newGrid[iMaxRows][iMaxCols];
for (int iRow=0; iRow<iMaxRows; ++iRow)
{
for (int iCol=0; iCol<iMaxCols; ++iCol)
newGrid[iMaxRow-iRow-1][iMaxCols-iCol-1] = oldGrid[iRow][iCol];
}
+
// 270°
Pixels newGrid[iMaxCols][iMaxRows];
for (int iRow=0; iRow<iMaxRows; ++iRow)
{
for (int iCol=0; iCol<iMaxCols; ++iCol)
newGrid[iMaxCols-iCol-1][iRow] = oldGrid[iRow][iCol];
}
Related
I have to dividing the irregular shape into decreasing rectangles, one the largest and then decreasing rectangles. I just wondering if such problem is known in the coding world? How to do it?
The drawing is showing what I would like to achieve:
First rectangle can be found with this piece of code taken from here
Rect findMinRect(const Mat1b& src)
{
Mat1f W(src.rows, src.cols, float(0));
Mat1f H(src.rows, src.cols, float(0));
Rect maxRect(0, 0, 0, 0);
float maxArea = 0.f;
for (int r = 0; r < src.rows; ++r)
{
for (int c = 0; c < src.cols; ++c)
{
if (src(r, c) == 0)
{
H(r, c) = 1.f + ((r > 0) ? H(r - 1, c) : 0);
W(r, c) = 1.f + ((c > 0) ? W(r, c - 1) : 0);
}
float minw = W(r, c);
for (int h = 0; h < H(r, c); ++h)
{
minw = min(minw, W(r - h, c));
float area = (h + 1) * minw;
if (area > maxArea)
{
maxArea = area;
maxRect = Rect(Point(c - minw + 1, r - h), Point(c + 1, r + 1));
}
}
}
}
return maxRect;
}
Usage is simple
Mat src, src_gray, src_bin;
cv::cvtColor(src, src_gray, COLOR_BGR2GRAY);
src_bin = (src_gray > 1) * 255;
Rect box = findMinRect(~src_bin);
rectangle(src, box, Scalar(0, 0, 255), 2);
cv::imshow("with_rectangle", src);
cv::waitKey();
But it's only first iteration. In next iteration we have to deal with at least 3 original image pieces, then with 9 pieces, and so on. I thing so... I will be grateful for any further suggestions.
I took Paul Gray's answer and wrote an iterative process that consumes the source to find all the areas from largest to smallest in pure c++, the filled portions of the matrix do not need to be a contiguous shape.
This is the rect class that I used, I found out after that there is already a RECT class in windef.h but whatever I had already made my own at that point.
class rect
{
public:
uint32_t rowStart;
uint32_t colStart;
uint32_t height;
uint32_t width;
rect(uint32_t _rowStart, uint32_t _colStart, uint32_t _height, uint32_t _width);
rect();
};
this is the algorithm Paul Gray posted in their answer redone without using openCV
bool findMinRect(std::vector<std::vector<bool>> *src, rect *retItem)
{
bool contFlg = false;
uint32_t srcRows = src->size();
uint32_t srcCols = (*src)[0].size();
std::vector<std::vector<float>> W;
W.resize(srcRows);
for (int row = 0; row < srcRows; row++)
{
for (int col = 0; col < srcCols; col++)
{
W[row].push_back(0.f);
}
}
std::vector<std::vector<float>> H;
H.resize(srcRows);
for (int row = 0; row < srcRows; row++)
{
for (int col = 0; col < srcCols; col++)
{
H[row].push_back(0.f);
}
}
rect maxRect(0, 0, 0, 0);
float maxArea = 0.f;
for (int r = 0; r < srcRows; ++r)
{
for (int c = 0; c < srcCols; ++c)
{
if ((*src)[r][c] == true)
{
H[r][c] = 1.f + ((r > 0) ? H[r-1][c] : 0);
W[r][c] = 1.f + ((c > 0) ? W[r][c-1] : 0);
}
float minw = W[r][c];
for (int h = 0; h < H[r][c]; ++h)
{
minw = min(minw, W[r - h][c]);
float area = (h + 1) * minw;
if (area > maxArea)
{
maxArea = area;
maxRect = rect(r - h, c - minw + 1, (r + 1 - (r - h)), (c + 1 - (c - minw + 1)));
contFlg = true;
}
}
}
}
*retItem = maxRect;
return contFlg;
}
The shapeChk method below is a sanity check for the source matrix to make sure is rectangular, if not it identifies where the issue is.
int shapeChk(std::vector<std::vector<bool>> *src)
{
uint32_t srcRows = src->size();
if (srcRows == 0)
{
return -1;
}
uint32_t srcCols = (*src)[0].size();
if (srcCols == 0)
{
return -2;
}
for (int row = 0; row < srcRows; row++)
{
if ((*src)[row].size() != srcCols)
{
return (-4 - (row + 1));
}
}
return 0;
}
The following method removes the area found from the source matrix in order to rerun the findMinRect method.
void clearclump(std::vector<std::vector<bool>> *src, rect *area)
{
for (int r = (area->rowStart); r < (area->rowStart + area->height); r++)
{
for (int c = (area->colStart); c < (area->colStart + area->width); c++)
{
(*src)[r][c] = false;
}
}
}
And here is the main method that puts it all together.
int areaClump(std::vector<std::vector<bool>> *src, std::vector<rect> *areas)
{
rect retItem(1, 0, 0, 0);
int err = shapeChk(src);
if (err != 0)
{
return err;
}
while (findMinRect(src, &retItem))
{
areas->push_back(retItem);
clearclump(src, &retItem);
}
return 0;
}
When you run areaClump the source matrix will be entirely consumed and will have all flase entries, while the areas vector contains all the found areas.
Not sure if this works for your purposes or if it's even the best way to handle it, but it worked for me so I thought I'd share the results, may be it'll help someone.
I am using opencv to output webcam to the screen.
I output the screen normally.
However, there is one problem.
Noise is generated in the result.
I do not know the reason.
Tell me how to do it
.
The following is the initialization function.
The imshow function was called to test.
void CRtspMgr::Init() {
// init video capture
if (m_capture == NULL) {
m_capture = new VideoCapture();
}
// set address
m_capture->open(0);
namedWindow("video", 1);
}
And it is the part that draws the screen.
void CRtspMgr::OnPaint()
{
// draw video mat
CPaintDC dc(this);
if (m_capture != NULL) {
if (m_capture->isOpened()) {
RECT r;
GetClientRect(&r);
cv::Size winSize(r.right, r.bottom);
Mat mat;
m_capture->read(mat);
// test
imshow("video", mat);
int bpp = 8 * mat.elemSize();
assert((bpp == 8 || bpp == 24 || bpp == 32));
BITMAPINFO bitInfo;
bitInfo.bmiHeader.biBitCount = bpp;
bitInfo.bmiHeader.biWidth = mat.cols;
bitInfo.bmiHeader.biHeight = mat.rows;
bitInfo.bmiHeader.biPlanes = 1;
bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitInfo.bmiHeader.biCompression = BI_RGB;
bitInfo.bmiHeader.biClrImportant = 0;
bitInfo.bmiHeader.biClrUsed = 0;
bitInfo.bmiHeader.biSizeImage = 0;
bitInfo.bmiHeader.biXPelsPerMeter = 0;
bitInfo.bmiHeader.biYPelsPerMeter = 0;
int destx = 0, desty = 0;
int destw = winSize.width;
int desth = winSize.height;
int imgx = 0, imgy = 0;
int imgWidth = mat.cols;
int imgHeight = mat.rows;
StretchDIBits(dc,
destx, desty, destw, desth,
imgx, imgy, imgWidth, imgHeight,
mat.data, &bitInfo, DIB_RGB_COLORS, SRCCOPY);
}
}
Invalidate();
}
show image
I do school project on basis of Atari Breakout. I do it in c++ using ALLEGRO 5 library. I don't know how to avoid one problem. When I compile my program i have flickering image on my program and the ball is going too slow. I was trying to change FPS but it still doesn't work properly.
Main loop is collecting informations about position of mouse (paddle), position of ball and bricks left. Than it should print it smoothly on the screen. Do you have any ideas, what i did wrong?
Here is my code:
#include <allegro5/allegro.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#define BRICKS_HORIZONTALLY 10
#define BRICKS_VERTICALLY 5
#define BRICK_WIDTH 102
#define BRICK_HEIGHT 50
#define BRICK_GAP 2
#define PADDLE_WIDTH 100
#define PADDLE_HEIGHT 20
#define PADDLE_POSITION_Y (768-50)
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
struct ball_typ {
int xv, yv;
int x, y;
};
int playerPaddleEdgeX;
ball_typ ball;
int bricks[BRICKS_VERTICALLY][BRICKS_HORIZONTALLY];
void makeScreenBlack() { //clead screen to black
al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display();
}
void resetBricks() {
for (int i = 0; i<BRICKS_VERTICALLY; i++) {
for (int ii = 0; ii<BRICKS_HORIZONTALLY; ii++) {
bricks[i][ii] = 1;
}
}
}
int numberOfBricksRemaining() {
int numberOfBricksRemaining;
numberOfBricksRemaining = 0;
for (int i = 0; i<BRICKS_VERTICALLY; i++) {
for (int ii = 0; ii<BRICKS_HORIZONTALLY; ii++) {
if (bricks[i][ii] != 0) {
numberOfBricksRemaining++;
}
}
}
return numberOfBricksRemaining;
}
void drawingThings() {
makeScreenBlack();
// draw the bricks
for (int i = 0; i<BRICKS_VERTICALLY; i++) {
for (int ii = 0; ii<BRICKS_HORIZONTALLY; ii++) {
if (bricks[i][ii] != 0) {
al_draw_filled_rectangle(ii*BRICK_WIDTH, i*BRICK_HEIGHT,(ii + 1)*BRICK_WIDTH - BRICK_GAP, (i + 1)*BRICK_HEIGHT - BRICK_GAP, al_map_rgb(255, 0, 0));
}
}
}
// draw the ball
al_draw_filled_circle(ball.x, ball.y, 8, al_map_rgb(255, 255, 255));
// draw the player
al_draw_filled_rectangle(playerPaddleEdgeX, PADDLE_POSITION_Y,playerPaddleEdgeX + PADDLE_WIDTH, PADDLE_POSITION_Y + PADDLE_HEIGHT, al_map_rgb(255, 255, 255));
ALLEGRO_FONT *font = al_create_builtin_font();
al_draw_textf(font, al_map_rgb(0, 0, 0), 10, 10, 0, "Player Position (playerPaddleEdgeX is %i)", playerPaddleEdgeX);
al_draw_textf(font, al_map_rgb(0, 0, 0), 10, 20, 0, "ball (x,y) position is (%i, %i)", ball.x, ball.y);
}
void resetBall() {
ball.x = 1024 / 2;
ball.y = 768 / 2;
ball.xv = 4;
ball.yv = 2;
}
int doesOverlap(int objectX, int objectY,
int areaLeft, int areaTop,
int areaRight, int areaBottom) {
if (ball.x > areaLeft &&
ball.x < areaRight &&
ball.y > areaTop &&
ball.y < areaBottom) {
return 1; // 1 here means yes
}
return 0; // 0 here means no
}
void moveBall() {
// update the ball's position for the next frame
ball.x += ball.xv;
ball.y += ball.yv;
// if the ball is overlapping the rectangle
if (ball.yv > 0) { // only if the ball is moving down
if (doesOverlap(ball.x, ball.y,
playerPaddleEdgeX + (PADDLE_WIDTH*0.0),
PADDLE_POSITION_Y,
playerPaddleEdgeX + (PADDLE_WIDTH*0.25),
PADDLE_POSITION_Y + PADDLE_HEIGHT) == 1) {
ball.xv = -5;
ball.yv = -3;
}
if (doesOverlap(ball.x, ball.y,
playerPaddleEdgeX + (PADDLE_WIDTH*0.25),
PADDLE_POSITION_Y,
playerPaddleEdgeX + (PADDLE_WIDTH*0.5),
PADDLE_POSITION_Y + PADDLE_HEIGHT) == 1) {
ball.xv = -3;
ball.yv = -5;
}
if (doesOverlap(ball.x, ball.y,
playerPaddleEdgeX + (PADDLE_WIDTH*0.5),
PADDLE_POSITION_Y,
playerPaddleEdgeX + (PADDLE_WIDTH*0.75),
PADDLE_POSITION_Y + PADDLE_HEIGHT) == 1) {
ball.xv = 3;
ball.yv = -5;
}
if (doesOverlap(ball.x, ball.y,
playerPaddleEdgeX + (PADDLE_WIDTH*0.75),
PADDLE_POSITION_Y,
playerPaddleEdgeX + (PADDLE_WIDTH*1.0),
PADDLE_POSITION_Y + PADDLE_HEIGHT) == 1) {
ball.xv = 5;
ball.yv = -3;
}
}
for (int i = 0; i<BRICKS_VERTICALLY; i++) {
for (int ii = 0; ii<BRICKS_HORIZONTALLY; ii++) {
if (bricks[i][ii] != 0) { // is the brick still here?
if (doesOverlap(ball.x, ball.y,
ii*BRICK_WIDTH, i*BRICK_HEIGHT,
(ii + 1)*BRICK_WIDTH - BRICK_GAP,
(i + 1)*BRICK_HEIGHT - BRICK_GAP) == 1) {
// reverse ball's vertical direction
ball.yv = -ball.yv;
bricks[i][ii] = 0; // erase the brick
}
}
}
}
// bounce off edges of screen
if (ball.x > 1024) {
ball.xv = -ball.xv;
}
if (ball.x < 0) {
ball.xv = -ball.xv;
}
if (ball.y < 0) {
ball.yv = -ball.yv;
}
// but reset ball if it goes off bottom of screen
if (ball.y > 768) {
// lose!
ALLEGRO_MOUSE_STATE state;
al_get_mouse_state(&state);
if (state.buttons & 1) { //reappear ball
resetBall();
}
}
}
void updatePaddlePosition() {
// for now, put the player's paddle where the mouse is
int pos_x = 1024 / 2;
ALLEGRO_MOUSE_STATE state;
al_get_mouse_state(&state);
pos_x = state.x;
playerPaddleEdgeX = pos_x;
}
void gameSetup() {
resetBricks();
resetBall();
// start with the ball off the bottom of the screen
ball.y = 768 + 50;
}
int main() {
ALLEGRO_TIMER * timer = NULL;
int FPS = 60;
al_init(); // allegro initializing
al_init_font_addon();
al_init_primitives_addon();
if (!al_init()){ //check
al_show_native_message_box(NULL, NULL, NULL,
"failed to initialize allegro!", NULL, NULL);
return -1;
}
timer = al_create_timer(1.0 / FPS);
al_install_keyboard();
al_install_mouse();
event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_mouse_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_set_new_window_position(20, 30);
display = al_create_display(1024, 768);
if (!display){ //check
al_show_native_message_box(NULL, NULL, NULL,
"failed to initialize display!", NULL, NULL);
return -1;
}
makeScreenBlack();
updatePaddlePosition();
gameSetup();
ALLEGRO_KEYBOARD_STATE key;
al_start_timer(timer);
while (al_key_down(&key, ALLEGRO_KEY_ESCAPE)){
updatePaddlePosition();
moveBall();
if (numberOfBricksRemaining() == 0) {
resetBricks();
}
drawingThings();
al_flip_display();
al_rest(0.01);
}
al_destroy_display(display);
return 0;
}
You're calling al_flip_display twice. You should remove the call to al_flip_display in makeScreenBlack() and keep the other one.
I'm trying to get every frame of the stream produced by the RGB camera of the Kinect (using SDK version 1.8) into an OpenCV (2.4.10) Mat_<Vec3b>. This is my current algorithm, which is not at all fast:
Mat_<Vec3b> mat = Mat::zeros(480, 640, CV_8UC3);
NUI_IMAGE_FRAME imageFrame;
NUI_LOCKED_RECT lockedRect;
if (sensor->NuiImageStreamGetNextFrame(colorStream, 0, &imageFrame) < 0) { return; }
INuiFrameTexture* texture = imageFrame.pFrameTexture;
texture->LockRect(0, &lockedRect, NULL, 0);
if (lockedRect.Pitch != 0)
{
BYTE* upperLeftCorner = (BYTE*)lockedRect.pBits;
BYTE* pointerToTheByteBeingRead = upperLeftCorner;
for (int i = 0; i < 480; i++)
{
for (int j = 0; j < 640; j++)
{
unsigned char r = *pointerToTheByteBeingRead;
pointerToTheByteBeingRead += 1;
unsigned char g = *pointerToTheByteBeingRead;
pointerToTheByteBeingRead += 1;
unsigned char b = *pointerToTheByteBeingRead;
pointerToTheByteBeingRead += 2; //So to skip the alpha channel
mat.at<Vec3b>(Point(j, i))[0] = r;
mat.at<Vec3b>(Point(j, i))[1] = g;
mat.at<Vec3b>(Point(j, i))[2] = b;
}
}
}
texture->UnlockRect(0);
sensor->NuiImageStreamReleaseFrame(colorStream, &imageFrame);
I checked the OpenCV documentation and I understand I'm supposed to use pointer access to increase efficiency. Are Mat_<Vec3b>s stored into memory the same way as Mats or should I do some other pointer arithmetic?
Also, I understand updating every single pixel every time is not the most efficient way of achieving the display of the stream through a Mat. What other things could I do?
Finally figured out how to use pointer arithmetic. The code is self-explanatory:
Mat_<Vec3b> mat = Mat::zeros(480, 640, CV_8UC3);
NUI_IMAGE_FRAME imageFrame;
NUI_LOCKED_RECT lockedRect;
if (sensor->NuiImageStreamGetNextFrame(colorStream, 0, &imageFrame) < 0) { return; }
INuiFrameTexture* texture = imageFrame.pFrameTexture;
texture->LockRect(0, &lockedRect, NULL, 0);
if (lockedRect.Pitch != 0)
{
BYTE* upperLeftCorner = (BYTE*)lockedRect.pBits;
BYTE* pointerToTheByteBeingRead = upperLeftCorner;
for (int i = 0; i < 480; i++)
{
Vec3b *pointerToRow = mat.ptr<Vec3b>(i);
for (int j = 0; j < 640; j++)
{
unsigned char r = *pointerToTheByteBeingRead;
pointerToTheByteBeingRead += 1;
unsigned char g = *pointerToTheByteBeingRead;
pointerToTheByteBeingRead += 1;
unsigned char b = *pointerToTheByteBeingRead;
pointerToTheByteBeingRead += 2; //So to skip the alpha channel
pointerToRow[j] = Vec3b(r, g, b);
}
}
}
texture->UnlockRect(0);
sensor->NuiImageStreamReleaseFrame(colorStream, &imageFrame);
I am getting an "Access violation reading location" error in my program when trying to access
elements in my 4D dynamic array.
Here is my allocation code
void mazeGen(int**** mazes, int width, int height,stack<SDL_Point> backtracker)
{
numberOfCalls++;
//first choose a starting location for the maze
//starting location must be odd in order to ensure that the generator does not go outside
//the maze bounds.
//allocate memory for the mazes
mazes = new int***[NUMHOR];
for (int i = 0; i < NUMVER; i++)
{
mazes[i] = new int**[NUMVER];
}
//allocate memory for the actual mazes
for (int x = 0; x < NUMHOR; x++)
{
for (int y = 0; y < NUMVER; y++)
{
mazes[x][y] = initMaze(WIDTH, HEIGHT);
}
}
//mazeGenHelper(maze, height, width, backtracker, start);
bool leftToRight = true;
for (int x = 0; x < NUMHOR; x++)
{
for (int y = 0; y < NUMVER; y++)
{
//generate mazes
SDL_Point* start = new SDL_Point();
//genx
do
{
start->x = generateRandomRange(1, width - 1);
} while (start->x % 2 == 0);
//gen y
do
{
start->y = generateRandomRange(1, height - 1);
} while (start->y % 2 == 0);
//empty stack
while (!backtracker.empty())
{
backtracker.pop();
}
mazeGenHelper(mazes[x][y], HEIGHT, WIDTH, backtracker, start);
//delete start to prevent memory leak
delete start;
}
}
}
Heres the rest of it(its a maze generation program in case you could not tell)
void mazeGenHelper(int** maze, int height, int width, stack<SDL_Point> backtracker, SDL_Point* point,SDL_Point* endPoint)
{
numberOfCalls++;
array<int, 4> directions = shuffleDirections();
for (int i = 0; i < 4; i++)
{
switch (directions[i])
{
case 1://up
{
if (point->y - 2 > 0 && maze[point->x][point->y - 2] == 1)
{
//delete maze walls
maze[point->x][point->y - 1] = 0;
maze[point->x][point->y - 2] = 0;
//add current point to the backtracker
SDL_Point newPoint = { point->x, point->y };
backtracker.push(newPoint);
//move the current point
point->y -= 2;
mazeGenHelper(maze, height, width, backtracker, point,endPoint);
}
}
case 2://right
{
if (point->x + 2 <width && maze[point->x+2][point->y] == 1)
{
//delete maze walls
maze[point->x+1][point->y] = 0;
maze[point->x+2][point->y] = 0;
//add current point to the backtracker
SDL_Point newPoint = { point->x, point->y };
backtracker.push(newPoint);
//move the current point
point->x += 2;
mazeGenHelper(maze, height, width, backtracker, point,endPoint);
}
}
case 3://down
{
if (point->y + 2 < height && maze[point->x][point->y + 2] == 1)
{
//delete maze walls
maze[point->x][point->y + 1] = 0;
maze[point->x][point->y + 2] = 0;
//add current point to the backtracker
SDL_Point newPoint = { point->x, point->y };
backtracker.push(newPoint);
//move the current point
point->y += 2;
mazeGenHelper(maze, height, width, backtracker, point,endPoint);
}
}
case 4://left
{
if (point->x - 2 > 0 && maze[point->x - 2][point->y] == 1)
{
//delete maze walls
maze[point->x - 1][point->y] = 0;
maze[point->x - 2][point->y] = 0;
//add current point to the backtracker
SDL_Point newPoint = { point->x, point->y };
backtracker.push(newPoint);
//move the current point
point->x -= 2;
mazeGenHelper(maze, height, width, backtracker, point,endPoint);
}
}
}
}
if (backtracker.size() != 0)
{
//pop curent element off the stack and recall
SDL_Point newPoint = backtracker.top();
endPoint->x = newPoint.x;
endPoint->x = newPoint.y;
backtracker.pop();
mazeGenHelper(maze, height, width, backtracker, &newPoint,endPoint);
}
// else the maze must be done
}
And here is me trying to access it
void sdlapp::render()
{
//clear the screen
SDL_RenderClear(m_renderer);
//do render stuff here
//rect area for
SDL_Rect rect = { 0,0, zoomLevel, zoomLevel };
//render the maze walls
for (int i = 0; i < WIDTH;i++)
for (int k = 0; k < HEIGHT; k++)
{
switch (maze[i][k])//<- thats where i am trying to access it.
{
case 1: // theres a wall
{
rect.x = i * zoomLevel-camera->x;
rect.y = k * zoomLevel-camera->y;
SDL_RenderCopy(m_renderer, mazeWallTex, NULL, &rect);
}
break;
case 2: //theres a start point
{
rect.x = i * zoomLevel - camera->x;
rect.y = k * zoomLevel - camera->y;
SDL_RenderCopy(m_renderer, mazeStartTex, NULL, &rect);
}
case 3:
{
rect.x = i * zoomLevel - camera->x;
rect.y = k * zoomLevel - camera->y;
SDL_RenderCopy(m_renderer, mazeEndTex, NULL, &rect);
}
}
}
//update the screen to the current render
SDL_RenderPresent(m_renderer);
}
I don't expect you to read through all of this code but i posted it all anyway. If anyone knows what i am doing wrong could you point me in the right direction?
Thank you for your time
JustinWeq,
BTW I dont really want to use vectors and can properly deal locate me memory with absoluty no memory leaks.
Since it solved the problem, I’ll copy my comment here as an answer (to get the “unanswered” list a bit cleaner):
I haven’t read the whole code, but the first thing standing out is this:
You pass in an int**** mazes to mazeGen, but the first thing that function does is to discard the value passed in and replace it with a fresh allocation.
If you want these allocations to be visible to the caller (which I assume you do; as it stands, that memory just leaks), you need to use int**** &maze. (And I still think you’d be better off without raw pointers, but this isn’t codereview.SE.)