Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:29:29+00:00 2026-05-30T18:29:29+00:00

I am trying to make a something that will draw lines on a pixel

  • 0

I am trying to make a something that will draw lines on a pixel grid, then on to a bitmap. I can (pretty much) get my the desired effect by reallocating the memory with ‘new’ (though the array is to big to delete[] so I end up with a memory leak).

My problem is, that I cannot for the life of me, work out why doing:

for (register unsigned int i = 0; i < WIDTH + HEIGHT * 4; i++)
{
    previewedPixels[i] = 0;
}

has a different effect than doing

previewedPixels = new sf::Uint8[WIDTH * HEIGHT * 4];

This is the rest of the code, to put it in perspective (the line that reallocates the memory and causes a memory leak is commented). Please excuse the mess :p

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <vector>
#include <iostream>

#define WIDTH 800
#define HEIGHT 600

class element
{
public:
    unsigned char value;
    unsigned char red;
    unsigned char green;
    unsigned char blue;
};

const int WIDTHxHEIGHT = WIDTH * HEIGHT;
std::vector< std::vector<short> >XandY(WIDTHxHEIGHT * 2, std::vector<short>(2, 0));                                     //This is the x and y values for the arrays below
sf::Uint8 *mainPixels      = new sf::Uint8[WIDTH * HEIGHT * 4];
sf::Uint8 *previewedPixels = new sf::Uint8[WIDTH * HEIGHT * 4];    //Previewed pixel co-        ordinates
sf::Uint8 *pixels          = new sf::Uint8[WIDTH * HEIGHT * 4];  //Put the preview and      main pixel grids together on this
element   *currentElement;  //Value of the element currently selected
/*****Declare Elements Here*****/

element metal;

void elementInit()
{
    metal.value    =   1;
    metal.blue     =  90;
    metal.red      =  90;
    metal.green    =  90;
}

inline void wipe()
{
    for (register unsigned int i = 0; i < WIDTH + HEIGHT * 4; i++)
    {
        previewedPixels[i] = 0;
    }
}

inline void draw(short x, short y, unsigned char r, unsigned char g, unsigned char b,     bool preview)
{
    if (preview)
    {
        previewedPixels[(x + WIDTH * y) * 4 + 0] = r;
        previewedPixels[(x + WIDTH * y) * 4 + 1] = g;
        previewedPixels[(x + WIDTH * y) * 4 + 2] = b;
        previewedPixels[(x + WIDTH * y) * 4 + 3] = 255;
    }
    else
    {
        mainPixels[(x + WIDTH * y) * 4 + 0] = r;
        mainPixels[(x + WIDTH * y) * 4 + 1] = g;
        mainPixels[(x + WIDTH * y) * 4 + 2] = b;
        mainPixels[(x + WIDTH * y) * 4 + 3] = 255;
    }
}

void display(void *UserData)
{

}

void lineDraw(short x1, short y1, short x2, short y2, bool preview)
{
    short xDiff = std::abs(x1 - x2);  //Difference between the two 'x' co-ordinates
    short yDiff = std::abs(y1 - y2);   //and the two y co-ordinates
    unsigned char red, green, blue;
    red   = currentElement->blue;
    green = currentElement->green;
    blue  = currentElement->blue;

    if (xDiff > yDiff && x1 < x2)   //Which quadrant it's in. This one is horizontal     going right
    {
        if (preview)        //If it is a preview then then put it onto a different array     of mainPixels
        {
            for (short i = x1; i <= x2; i++)
                draw(i, y1, red, green, blue, true);
        }
        else                //If it's not a preview then just go ahaid on the normal     array
        {
            for (short i = x1; i <= x2; i++)
                draw(i, y1, red, green, blue, false);
        }
    }
    else if (xDiff > yDiff && x1 > x2)  //Horizontal going left
    {
        if (preview)
        {
            for (short i = x1; i >= x2; i--)
                draw(i, y1, red, green, blue, true);
        }
        else
        {
            for (short i = x1; i >= x2; i--)
                draw(i, y1, red, green, blue, false);
        }
    }
    else if (xDiff < yDiff && y1 > y2)  //Going down
    {
        if (preview)
        {
            for (short i = y1; i >= y2; i--)
                draw(x1, i, red, green, blue, true);
        }
        else
        {
            for (short i = y1; i >= y2; i--)
                draw(x1, i, red, green, blue, false);
        }
    }
    else if (xDiff < yDiff && y1 < y2)  //Going Up
    {
        if (preview)
        {
            for (short i = y1; i <= y2; i++)
                draw(x1, i, red, green, blue, true);
        }
        else
        {
            for (short i = y1; i <= y2; i++)
                draw(x1, i, red, green, blue, false);
        }
    }
}


int main()
{
    //Inititialization stuff
    for (unsigned int i = 0; i < WIDTHxHEIGHT * 2; i++)
    {
        XandY[i][0] = i % WIDTH;
        XandY[i][1] = i / WIDTH;
    }
    elementInit();  //Initialize the Elements with their values
    currentElement = &metal;  //By default, select metal
    sf::RenderWindow App(sf::VideoMode(WIDTH, HEIGHT, 32), "ElectroToy");
    sf::Thread graphics(&display, &App);    //Create the thread that will deal with the     graphics
    graphics.Launch();      //Launch Graphics thread
    sf::Image screen(WIDTH, HEIGHT);
    sf::Sprite sprite, prevSprite;  //The first one is the main picture, the other is     the one for the preview mainPixels
    sf::Event Event;
    const sf::Input & Input = App.GetInput();
    short mouseX = Input.GetMouseX();
    short mouseY = Input.GetMouseY();
    short oldMouseX = mouseX;
    short oldMouseY = mouseY;
    bool running = true;
    //End of Initialization stuff
    while (running)                 //main loop
    {
        mouseX = Input.GetMouseX();
        mouseY = Input.GetMouseY();
    bool leftMouseDown = Input.IsMouseButtonDown(sf::Mouse::Left);
    bool rightMouse = Input.IsMouseButtonDown(sf::Mouse::Right);
    bool LShift = Input.IsKeyDown(sf::Key::LShift);
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
            {
                App.Close();
                running = false;
            }
        }
        if (leftMouseDown)
        {
            oldMouseX = mouseX;     //Remember the X,Y co-ordinates when the button was     pressed
            oldMouseY = mouseY;
            while (leftMouseDown)
            {
                while (App.GetEvent(Event));    //recieve events so it doesn't get stuck     in an infinite loop
                leftMouseDown = Input.IsMouseButtonDown(sf::Mouse::Left);   //update mouse button status
                mouseX = Input.GetMouseX();
                mouseY = Input.GetMouseY();
                lineDraw(oldMouseX, oldMouseY, mouseX, mouseY, true);

                for (register unsigned int i = 0; i < WIDTHxHEIGHT * 4; i++)  //Combine     the two pixel arrays to create what will go on the screen
                {
                    pixels[i] = mainPixels[i];
                    pixels[i] = previewedPixels[i];
                }
                wipe();
                //previewedPixels = new sf::Uint8[WIDTH * HEIGHT * 4];
                screen.LoadFromPixels(WIDTH, HEIGHT, pixels);
                sprite.SetImage(screen);
                App.Clear();
                App.Draw(sprite);
                App.Display();
            }
            lineDraw(oldMouseX, oldMouseY, mouseX, mouseY, false);
        }
        oldMouseX = mouseX;
        oldMouseY = mouseY;
        screen.LoadFromPixels(WIDTH, HEIGHT, mainPixels);
        sprite.SetImage(screen);
        App.Clear();
        App.Draw(sprite);
        App.Display();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:29:30+00:00Added an answer on May 30, 2026 at 6:29 pm

    Well,

    for (register unsigned int i = 0; i < WIDTH + HEIGHT * 4; i++)
    

    uses bounds different from

    previewedPixels = new sf::Uint8[WIDTH * HEIGHT * 4];
    

    Is that it?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m trying to make something that will take lines of input from the user,
I'm trying to make something like a spellchecker, that will list possible words under
I am trying to make an if statement in javascript that will do something
I'm trying to create a user control that allows users to make something like
Trying to make a make generic select control that I can dynamically add elements
i'm trying to make a program that will connect two points that i click
I am trying to make a function that will determine if value in a
I'm trying to make something so that it takes a Book and a string
I am trying to make a function in C that will print a string
I am trying to make a function in PHP that will allow me to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.