tutorial:framebuffer_graphics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorial:framebuffer_graphics [2020/11/12 21:45]
iridescence [C/C++]
tutorial:framebuffer_graphics [2021/03/12 16:10] (current)
neo Bounds copy and paste errors
Line 59: Line 59:
         x = 480;         x = 480;
     }     }
-    if(y > 480) { +    if(y > 272) { 
-        y = 480;+        y =272;
     }     }
  
Line 81: Line 81:
  
 **gfx.hpp** **gfx.hpp**
-<code>+<code C++>
 #include <cstdint> #include <cstdint>
  
Line 93: Line 93:
  
 **gfx.cpp** **gfx.cpp**
-<code>+<code C++>
 #include "gfx.hpp" #include "gfx.hpp"
 #include <pspge.h> #include <pspge.h>
Line 173: Line 173:
 pub struct Renderer { pub struct Renderer {
     draw_buffer: *mut u32,     draw_buffer: *mut u32,
-    disp_buffer: *mut u32+    disp_buffer: *mut u32,
 } }
  
 impl Renderer { impl Renderer {
     pub unsafe fn new() -> Self {     pub unsafe fn new() -> Self {
-        let res Self{ +        let draw_buffer = psp::sys::sceGeEdramGetAddr() as *mut u32; 
-            draw_buffer: psp::sys::sceGeEdramGetAddr() as *mut u32, +        let disp_buffer psp::sys::sceGeEdramGetAddr().add(512 * 272 * 4) as *mut u32;
-            disp_buffer: (psp::sys::sceGeEdramGetAddr() as u32 + 512 * 272 * 4) as *mut u32 +
-        };+
  
         psp::sys::sceDisplaySetMode(DisplayMode::Lcd, 480, 272);         psp::sys::sceDisplaySetMode(DisplayMode::Lcd, 480, 272);
-        psp::sys::sceDisplaySetFrameBuf(res.disp_buffer as *const u8, 512, DisplayPixelFormat::Psm8888, DisplaySetBufSync::NextFrame);+        psp::sys::sceDisplaySetFrameBuf( 
 +            disp_buffer as *const u8, 
 +            512, 
 +            DisplayPixelFormat::Psm8888, 
 +            DisplaySetBufSync::NextFrame
 +        );
  
-        return res;+        Self { 
 +            draw_buffer, 
 +            disp_buffer, 
 +        }
     }     }
  
-    pub fn clear(self: &Self, color: u32) { +    pub fn clear(&self, color: u32) { 
-        unsafe{ +        unsafe { 
-            for i in 0..(512 * 272{+            for i in 0..512 * 272 {
                 *self.draw_buffer.add(i as usize) = color;                 *self.draw_buffer.add(i as usize) = color;
             }             }
Line 197: Line 203:
     }     }
  
-    pub fn swap_buffers(self: &mut Self) { +    pub fn swap_buffers(&mut self) { 
-        unsafe{ +        core::mem::swap(&mut self.disp_buffer, &mut self.draw_buffer);
-            let tmp = self.disp_buffer+
-            self.disp_buffer = self.draw_buffer+
-            self.draw_buffer = tmp;+
  
 +        unsafe {
             psp::sys::sceKernelDcacheWritebackInvalidateAll();             psp::sys::sceKernelDcacheWritebackInvalidateAll();
-            psp::sys::sceDisplaySetFrameBuf(self.disp_buffer as *const u8, 512, DisplayPixelFormat::Psm8888, DisplaySetBufSync::NextFrame);+            psp::sys::sceDisplaySetFrameBuf( 
 +                self.disp_buffer as *const u8, 
 +                512, 
 +                DisplayPixelFormat::Psm8888, 
 +                DisplaySetBufSync::NextFrame
 +            );
         }         }
     }     }
  
-    pub fn draw_rect(self: &Self, x: usize, y: usize, w: usize, h: usize, color: u32) { +    pub fn draw_rect(&self, x: usize, y: usize, w: usize, h: usize, color: u32) { 
-        unsafe { +        for y1 in 0..h { 
-            for y1 in 0..h { +            for x1 in 0..w { 
-                for x1 in 0..w { +                if let Some(ptr) = self.calculate_offset(x + x1, y + y1) 
-                    match calculate_offset(self.draw_buffer, x + x1, y + y1) { +                    unsafe 
-                        Some(ptr) => { *ptr = color}, +                        *ptr = color;
-                        None => {}+
                     }                     }
                 }                 }
Line 220: Line 228:
         }         }
     }     }
-} 
  
-#[inline] +    #[inline] 
-unsafe fn calculate_offset(ptr: *mut u32, x: usize, y: usize) -> Option<*mut u32> { +    fn calculate_offset(&self, x: usize, y: usize) -> Option<*mut u32> { 
-    if x <= 480 && y <= 272 { +        unsafe { 
-        return Some(ptr.add(x + y * 512) as *mut u32); +            if x <= 480 && y <= 272 { 
-    }else{ +                Some(self.draw_buffer.add(x + y * 512) as *mut u32) 
-        return None+            } else { 
 +                None 
 +            } 
 +        }
     }     }
 } }
  • tutorial/framebuffer_graphics.1605217522.txt.gz
  • Last modified: 2020/11/12 21:45
  • by iridescence