I'm trying to convert an 8 byte integer to a 4 byte integer so that I can draw a cube on the screen.
The warning it gives me is:
Warning C26451 Arithmetic overflow: Using operator '*'
on a 4 byte
value and then casting the result to a 8 byte value. Cast the value to
the wider type before calling operator '*'
to avoid overflow
(io.2). somthingC:UsersAdminsource
epossomthingsomthing
enderer.cpp 79
I cant change the 4 byte int to an 8 byte int because it doesn't work with other parts of my code.
here is the code I'm using the error is on line 79:
#include <Windows.h>
struct Render_State
{
int width;
int hight;
void* memory;
BITMAPINFO bitmap_info;
};
Render_State render_state;
void render_backround(HWND hwnd,int colour)
{
if (WM_PAINT)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
unsigned int* pixel = (unsigned int*)render_state.memory;
for (int y = 0; y < render_state.hight; y+=1)
{
for (int x = 0; x < render_state.width; x+=1)
{
*pixel++ = colour;
}
}
// render
StretchDIBits(
hdc, 0, 0, render_state.width, render_state.hight, 0, 0, render_state.width, render_state.hight,
render_state.memory, &render_state.bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
EndPaint(hwnd, &ps);
}
}
void clear_screen(HWND hwnd, int colour)
{
if (WM_PAINT)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
unsigned int* pixel = (unsigned int*)render_state.memory;
for (int y = 0; y < render_state.hight; y += 1)
{
for (int x = 0; x < render_state.width; x += 1)
{
*pixel++ = colour;
}
}
// render
StretchDIBits(hdc,0,0,render_state.width,render_state.hight,
0,0,render_state.width,render_state.hight,
render_state.memory, &render_state.bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
EndPaint(hwnd, &ps);
}
}
void draw_rect(HWND hwnd, int X, int Y, int X2 , int Y2, int colour)
{
if (WM_PAINT)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
for (int y = Y; y <Y2; y += 1)
{
// line 79
unsigned int* pixel = (unsigned int*)render_state.memory + X + y * render_state.width;
for (int x = X; x < X; x += 1)
{
*pixel++ = colour;
}
}
// render
StretchDIBits(hdc, 0, 0, render_state.width, render_state.hight, 0, 0,
render_state.width, render_state.hight,
render_state.memory, &render_state.bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
EndPaint(hwnd, &ps);
}
}
question from:
https://stackoverflow.com/questions/65885992/how-to-convert-an-8-byte-integer-to-a-4-byte-integer