Live Sprites??

Started by habi2, Dec 25, 2024, 09:47 AM

Previous topic - Next topic

habi2


I actually using streams send a picture byte by byte (picture being .bmp) to client. On client side i created GUICanvas of VectorScreen(1,1) and treating it as a picture proceeded to draw the image. This is what i got.

However CPU went high. 40% usage and some 485 MB RAM also used.

And when i tried a full-screen picture (1920*1080), it took little time( like 30 seconds) to draw the picture. I saw the full picture. Then it crashed!!

Have any idea to slow CPU usage. The code is below
//image is blob or array containing bytes of a .bmp image as a whole.
function DrawBitMap(image,limit=2073600) //2073600=1920*1080
{
 // Get the image width and height from the header (starting at byte offset 18).
    local width = ((image[18] << 0) | (image[19] << 8) | (image[20] << 16) | (image[21] << 24));
    local height = ((image[22] << 0) | (image[23] << 8) | (image[24] << 16) | (image[25] << 24));
   
    // Read the bit count (information about whether an alpha channel is present).
    local biBitCount = (image[28] << 0) | (image[29] << 8);  // At byte 28, gives us biBitCount
   
    // Check if the bit count is 32 (indicating alpha channel is present)
    local has_alpha = false;
    if (biBitCount == 32) {
        has_alpha = true;
    }
   // Initialize the 2D array `pic` to hold the RGBA values
    pic <- array(height);
foreach(index,c in pic)
pic[index]=array(width);
   
// Data offset from the BMP header. Usually it's at byte 54, which is common for uncompressed BMP files.
    local data_offset = (image[10] << 0) | (image[11] << 8) | (image[12] << 16) | (image[13] << 24);
   
    // Start reading pixel data. The first pixel of the image data starts at byte [data_offset].
    local data_index = data_offset;  // Pointer to the pixel data.
   
    // Loop through each row and column to fill the `pic` array
    for (local row = 0; row < height; row++) {
for (local col = 0; col < width; col++) {
            // Read pixel data for one pixel
            local b = image[data_index++]; // Blue channel
            local g = image[data_index++]; // Green channel
            local r = image[data_index++]; // Red channel
           
            local a = 255; // Default to fully opaque if no alpha channel (for 24-bit BMP)
            if (has_alpha) {
                a = image[data_index++]; // Alpha channel (if present, for 32-bit BMP)
            }
            local canvas=GUICanvas();
canvas.Colour=Colour(r,g,b,a);
canvas.Size=VectorScreen(1,1);
canvas.Position=VectorScreen(col,height - 1 - row);
pic[row][col]=canvas;

       
if(limit)
if((row*col)>limit)return pic;
        }
    }
    return pic;
}

Should i use 3D flag?

Can chinese/russian characters be drawn this way and make other language chat..!!

MEGAMIND

Quote from: habi2 on Dec 25, 2024, 09:47 AM
I actually using streams send a picture byte by byte (picture being .bmp) to client. On client side i created GUICanvas of VectorScreen(1,1) and treating it as a picture proceeded to draw the image. This is what i got.

However CPU went high. 40% usage and some 485 MB RAM also used.

And when i tried a full-screen picture (1920*1080), it took little time( like 30 seconds) to draw the picture. I saw the full picture. Then it crashed!!

Have any idea to slow CPU usage. The code is below
//image is blob or array containing bytes of a .bmp image as a whole.
function DrawBitMap(image,limit=2073600) //2073600=1920*1080
{
 // Get the image width and height from the header (starting at byte offset 18).
    local width = ((image[18] << 0) | (image[19] << 8) | (image[20] << 16) | (image[21] << 24));
    local height = ((image[22] << 0) | (image[23] << 8) | (image[24] << 16) | (image[25] << 24));
   
    // Read the bit count (information about whether an alpha channel is present).
    local biBitCount = (image[28] << 0) | (image[29] << 8);  // At byte 28, gives us biBitCount
   
    // Check if the bit count is 32 (indicating alpha channel is present)
    local has_alpha = false;
    if (biBitCount == 32) {
        has_alpha = true;
    }
   // Initialize the 2D array `pic` to hold the RGBA values
    pic <- array(height);
foreach(index,c in pic)
pic[index]=array(width);
   
// Data offset from the BMP header. Usually it's at byte 54, which is common for uncompressed BMP files.
    local data_offset = (image[10] << 0) | (image[11] << 8) | (image[12] << 16) | (image[13] << 24);
   
    // Start reading pixel data. The first pixel of the image data starts at byte [data_offset].
    local data_index = data_offset;  // Pointer to the pixel data.
   
    // Loop through each row and column to fill the `pic` array
    for (local row = 0; row < height; row++) {
for (local col = 0; col < width; col++) {
            // Read pixel data for one pixel
            local b = image[data_index++]; // Blue channel
            local g = image[data_index++]; // Green channel
            local r = image[data_index++]; // Red channel
           
            local a = 255; // Default to fully opaque if no alpha channel (for 24-bit BMP)
            if (has_alpha) {
                a = image[data_index++]; // Alpha channel (if present, for 32-bit BMP)
            }
            local canvas=GUICanvas();
canvas.Colour=Colour(r,g,b,a);
canvas.Size=VectorScreen(1,1);
canvas.Position=VectorScreen(col,height - 1 - row);
pic[row][col]=canvas;

       
if(limit)
if((row*col)>limit)return pic;
        }
    }
    return pic;
}

Should i use 3D flag?

Can chinese/russian characters be drawn this way and make other language chat..!!
for characters yes i suppose but we might need all characters firsrt in an image form.. perhaps.. then draw them as u did with image (smaller this time).. though good work

vitovc

#2
Quote from: habi2 on Dec 25, 2024, 09:47 AMHave any idea to slow CPU usage.
After you created first picture by GUICanvas() once then you can change pixel's colors (canvas.Colour=)  instead creating new pixels by GUICanvas() each time. its only 'optimization' i know.
But anyway it will be hearvy for hardware, specially for old/low-end cpu.
...::: vice city :::...
Useful things for vcmp: Relative position and finding an angle (in 3d), 3d line (like laser)