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/13 20:40]
iridescence [Rust]
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
 +            );
         }         }
     }     }
  
-    #[inline] +    pub fn draw_rect(&self, x: usize, y: usize, w: usize, h: usize, color: u32
-    unsafe fn calculate_offset(ptr: *mut u32, x: usize, y: usize) -> Option<*mut u32+        for y1 in 0..h 
-        if x <= 480 && y <= 272 +            for x1 in 0..w { 
-            return Some(ptr.add(x + y * 512as *mut u32)+                if let Some(ptr) = self.calculate_offset(x + x1, + y1
-        }else{ +                    unsafe { 
-            return None+                        *ptr = color
 +                    } 
 +                
 +            }
         }         }
     }     }
  
-    pub fn draw_rect(self: &Self, x: usize, y: usize, w: usize, h: usize, color: u32{+    #[inline] 
 +    fn calculate_offset(&self, x: usize, y: usize) -> Option<*mut u32{
         unsafe {         unsafe {
-            for y1 in 0..h +            if x <= 480 && y <= 272 
-                for x1 in 0..w { +                Some(self.draw_buffer.add(x + y * 512) as *mut u32
-                    match calculate_offset(self.draw_bufferx + x1, + y1{ +            else 
-                        Some(ptr) => { *ptr = color}+                None
-                        None => {+
-                    } +
-                }+
             }             }
         }         }
     }     }
 } }
- 
 </code> </code>
  
  • tutorial/framebuffer_graphics.1605300007.txt.gz
  • Last modified: 2020/11/13 20:40
  • by iridescence