got basic screen working with pixel drawing and filling
This commit is contained in:
+69
-42
@@ -24,29 +24,30 @@ export fn main() void = {
|
||||
defer sdl3::DestroyRenderer(render);
|
||||
sdl3::SetRenderDrawColor(render, 0, 0, 128, 255)!;
|
||||
|
||||
const img = image::LoadTexture(render, c::nulstr("./assets/mascot.jpg\0"))!;
|
||||
defer sdl3::DestroyTexture(img);
|
||||
// const img = image::LoadTexture(render, c::nulstr("./assets/mascot.jpg\0"))!;
|
||||
// defer sdl3::DestroyTexture(img);
|
||||
|
||||
let w = 0.0f32, h = 0.0f32;
|
||||
sdl3::GetTextureSize(img, &w, &h)!;
|
||||
sdl3::get_error()!;
|
||||
const w = w: int, h = h: int;
|
||||
// let w = 0.0f32, h = 0.0f32;
|
||||
// sdl3::GetTextureSize(img, &w, &h)!;
|
||||
// sdl3::get_error()!;
|
||||
// const w = w: int, h = h: int;
|
||||
|
||||
let x = 0, y = 0;
|
||||
let xd = 1, yd = 1;
|
||||
// let x = 0, y = 0;
|
||||
// let xd = 1, yd = 1;
|
||||
|
||||
|
||||
const screen_w = WIDTH;
|
||||
const screen_h = HEIGHT;
|
||||
//Create blank texture for screen
|
||||
const meadow_texture = sdl3::CreateTexture(render,
|
||||
sdl3::PixelFormat::XRGB8888,
|
||||
sdl3::PixelFormat::XRGB4444,
|
||||
sdl3::TextureAccess::STATIC,
|
||||
screen_w,
|
||||
screen_h)!;
|
||||
defer sdl3::DestroyTexture(meadow_texture);
|
||||
// const pixeldata: [WIDTH * HEIGHT]u32 = [0...];
|
||||
let pixeldata: *[uxn::MAXWIDTH * uxn::MAXHEIGHT]u32 = alloc([0...])!; //TODO figure out actual size
|
||||
let pixeldata: *[uxn::MAXWIDTH * uxn::MAXHEIGHT]u16 = alloc([0...])!; //TODO figure out actual size
|
||||
defer free (pixeldata);
|
||||
|
||||
let run = true;
|
||||
let ev = sdl3::Event { ... };
|
||||
@@ -116,11 +117,24 @@ export fn main() void = {
|
||||
};
|
||||
if(state.screen_size_changed){
|
||||
let dims = uxn::get_window_size(state);
|
||||
fmt::printfln("Setting window to width: {:x} height: {:x}", dims.width, dims.height)!;
|
||||
// fmt::printfln("Setting window to width: {:x} height: {:x}", dims.width, dims.height)!;
|
||||
if((dims.width != 0) && (dims.height !=0)){
|
||||
//TODO, check current dims
|
||||
//TODO, check current dims
|
||||
//
|
||||
sdl3::DestroyTexture(meadow_texture);
|
||||
let w: int = dims.width: int;
|
||||
let h: int = dims.height: int;
|
||||
|
||||
fmt::printfln("Setting window to width: {} height: {}", w, h)!;
|
||||
sdl3::SetWindowSize(win,w,h)!;
|
||||
|
||||
meadow_texture = sdl3::CreateTexture(render,
|
||||
sdl3::PixelFormat::XRGB4444,
|
||||
sdl3::TextureAccess::STATIC,
|
||||
w,
|
||||
h)!;
|
||||
|
||||
sdl3::SetWindowSize(win,dims.width: int,dims.height: int)!;
|
||||
state.screen_update = true;
|
||||
};
|
||||
|
||||
|
||||
@@ -129,20 +143,33 @@ export fn main() void = {
|
||||
|
||||
if(state.screen_update){
|
||||
let pcount = 0;
|
||||
for(let x: u16 = 0; x < screen_w: u16; x+=1 ){
|
||||
for(let y: u16 = 0; y < screen_h: u16; y+=1 ){
|
||||
const colshort = uxn::get_color(x,y,state);
|
||||
const dims = uxn::get_window_size(state);
|
||||
for(let y: u16 = 0; y <= dims.height: u16; y+=1 ){
|
||||
for(let x: u16 = 0; x <= dims.width: u16; x+=1 ){
|
||||
let colshort: u16 = uxn::get_color(x,y,state);
|
||||
//take 16-bit color and transform to 32-bit number
|
||||
//XRGB -> XXRXGXBX
|
||||
const r = (colshort & 0x0F00): u32 << (3*4);
|
||||
const g = (colshort & 0x00F0): u32 << (2*4);
|
||||
const b = (colshort & 0x000F): u32 << (1*4);
|
||||
const color: u32 = r | g | b;
|
||||
// const r = (colshort & 0x0F00): u32 << (3*4);
|
||||
// const g = (colshort & 0x00F0): u32 << (2*4);
|
||||
// const b = (colshort & 0x000F): u32 << (1*4);
|
||||
// const color: u32 = r | g | b;
|
||||
// const data = pixeldata: *[*]u32; //
|
||||
pixeldata[pcount] = color;
|
||||
// if(colshort !=0x0fff){
|
||||
// fmt::printfln("Pixel x: {:x} y: {:x} color: {:x}", x, y, colshort)!;
|
||||
// };
|
||||
// fmt::printfln("Pixel x: {} y: {} color: {:x} pcount: {:x}", x, y, colshort, pcount)!;
|
||||
if(y == 0 && x < 64) colshort = 0;
|
||||
const index: u32 = y: u32 * dims.width: u32 + x: u32;
|
||||
// if(index < 10) fmt::printfln("pcount: {} index: {} x: {} y: {}", pcount, index, x, y)!;
|
||||
pixeldata[index] = colshort;
|
||||
pcount +=1;
|
||||
};
|
||||
// pixeldata[pcount] = 0;
|
||||
// pcount += 4096;
|
||||
};
|
||||
const pitch: int = dims.width: int * 2;
|
||||
fmt::printfln("Pitch is {}", pitch)!;
|
||||
if(pitch > 0) sdl3::UpdateTexture(meadow_texture, null, pixeldata, pitch)!;
|
||||
state.screen_update = false;
|
||||
};
|
||||
|
||||
@@ -156,33 +183,33 @@ export fn main() void = {
|
||||
|
||||
sdl3::RenderClear(render)!;
|
||||
|
||||
sdl3::RenderTexture(render, img, null, &sdl3::FRect {
|
||||
x = x: f32,
|
||||
y = y: f32,
|
||||
w = w: f32,
|
||||
h = h: f32,
|
||||
})!;
|
||||
|
||||
sdl3::UpdateTexture(meadow_texture, null, pixeldata: *opaque, 32)!;
|
||||
sdl3::RenderTexture(render, meadow_texture, null, null)!;
|
||||
|
||||
// sdl3::RenderTexture(render, img, null, &sdl3::FRect {
|
||||
// x = x: f32,
|
||||
// y = y: f32,
|
||||
// w = w: f32,
|
||||
// h = h: f32,
|
||||
// })!;
|
||||
|
||||
sdl3::RenderPresent(render)!;
|
||||
sdl3::Delay(1000 / 60);
|
||||
|
||||
x += xd;
|
||||
y += yd;
|
||||
if (x + w >= WIDTH) {
|
||||
xd = -1;
|
||||
};
|
||||
if (x <= 0) {
|
||||
xd = 1;
|
||||
};
|
||||
if (y + h >= HEIGHT) {
|
||||
yd = -1;
|
||||
};
|
||||
if (y <= 0) {
|
||||
yd = 1;
|
||||
};
|
||||
// x += xd;
|
||||
// y += yd;
|
||||
// if (x + w >= WIDTH) {
|
||||
// xd = -1;
|
||||
// };
|
||||
// if (x <= 0) {
|
||||
// xd = 1;
|
||||
// };
|
||||
// if (y + h >= HEIGHT) {
|
||||
// yd = -1;
|
||||
// };
|
||||
// if (y <= 0) {
|
||||
// yd = 1;
|
||||
// };
|
||||
|
||||
sdl3::get_error()!;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user