master chld/cotton_haiku / src / cottonwindow.cpp
 1#include <Application.h>
 2#include <Window.h>
 3#include <View.h>
 4#include <Bitmap.h>
 5
 6#include "cottonwindow.h"
 7
 8
 9class View:public BView
10{
11public:View(BRect f, BBitmap *bmp):
12  BView(f, "cottonview", B_FOLLOW_ALL, B_WILL_DRAW), bmp(bmp){}
13
14  void Draw(BRect r){
15    DrawBitmap(bmp, Bounds());
16  }
17  BBitmap *bmp;
18
19};
20
21class Win:public BWindow
22{
23public:Win(BRect f, const char *title, int texture_w, int texture_h):
24  BWindow(f, title, B_TITLED_WINDOW, B_WILL_DRAW){
25  bmp = new BBitmap(BRect(0, 0, texture_w - 1, texture_h - 1), B_RGB32);
26  view = new View(Bounds(), bmp);
27  AddChild(view);
28}
29
30  BBitmap *bmp;
31  View *view;
32};
33
34class App:public BApplication
35{
36public:App(BRect r, const char *title, int texture_w, int texture_h):BApplication("application/x-vnd.example.blank")
37  {
38    w = new Win(r, title, texture_w, texture_h);
39    w->Show();
40  }
41
42  Win *w;
43};
44
45static App *_app=nullptr;
46
47int cottonwindow_init(CottonWindow *cw, const char *title, int window_w, int window_h, int texture_w, int texture_h)
48{
49  (void)cw;
50  
51  _app=new App(BRect(100, 100, 100 + window_w, 100 + window_h), title, texture_w, texture_h);
52  
53  return 0;
54}
55
56void cottonwindow_update(CottonWindow *cw, const void *buffer, int pitch)
57{
58  (void)cw;
59
60  if(!_app) return;
61
62  if(_app->w->Lock()){
63    uint8 *d=(uint8*)_app->w->bmp->Bits();
64    int32 dP=_app->w->bmp->BytesPerRow();
65    int32 r=_app->w->bmp->Bounds().IntegerHeight()+1;
66
67    const uint8 *src=(const uint8*)buffer;
68
69    for(int32 y=0; y<r; y++){
70      memcpy(d+y*dP, src+y*pitch, std::min(dP, pitch));
71    }
72      _app->w->view->Invalidate();
73  _app->w->Unlock();
74  }
75}
76
77int cottonwindow_process_input(CottonWindow *cw, uint8_t *k)
78{
79  (void)cw;
80  (void)k;
81
82  // stub toe
83  return 0;
84}
85
86void cottonwindow_destroy(CottonWindow *cw)
87{
88  App *a=(App *)cw->app;
89  if (_app){
90    BMessenger(_app).SendMessage(B_QUIT_REQUESTED);
91  } 
92}