1 /* 2 * Copyright 2012, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Aaron Hill <serac@hillvisions.com> 7 * Alexander von Gluck <kallisti5@unixzen.com> 8 */ 9 10 11 #include "GLifeSaver.h" 12 13 #include <GLView.h> 14 #include <ScreenSaver.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <View.h> 18 19 #include "GLifeGrid.h" 20 #include "GLifeState.h" 21 #include "GLifeConfig.h" 22 #include "GLifeView.h" 23 24 25 // ------------------------------------------------------ 26 // GLifeSaver Class Constructor Definition 27 GLifeSaver::GLifeSaver(BMessage* pbmPrefs, image_id iidImage) 28 : 29 BScreenSaver(pbmPrefs, iidImage) 30 { 31 // Check for preferences 32 if (!pbmPrefs->IsEmpty()) 33 RestoreState(pbmPrefs); 34 35 // Seed random number generator 36 srandom(system_time()); 37 } 38 39 40 // ------------------------------------------------------ 41 // GLifeSaver Class SaveState Definition 42 status_t 43 GLifeSaver::SaveState(BMessage* pbmPrefs) const 44 { 45 return fGLifeState.SaveState(pbmPrefs); 46 } 47 48 49 // ------------------------------------------------------ 50 // GLifeSaver Class RestoreState Definition 51 void 52 GLifeSaver::RestoreState(BMessage* pbmPrefs) 53 { 54 fGLifeState.RestoreState(pbmPrefs); 55 } 56 57 58 // ------------------------------------------------------ 59 // GLifeSaver Class StartConfig Definition 60 void 61 GLifeSaver::StartConfig(BView* pbvView) 62 { 63 // Setup the "config" class 64 GLifeConfig* pglcConfig = new GLifeConfig(pbvView->Bounds(), 65 &fGLifeState); 66 67 pbvView->AddChild(pglcConfig); 68 } 69 70 71 // ------------------------------------------------------ 72 // GLifeSaver Class StartSaver Definition 73 status_t 74 GLifeSaver::StartSaver(BView* pbvView, bool bPreview) 75 { 76 if (bPreview) { 77 // We do not use the preview option 78 fGLifeViewport = 0; 79 return B_ERROR; 80 } else { 81 SetTickSize(c_iTickSize); 82 83 fGLifeViewport = new GLifeView(pbvView->Bounds(), 84 "GLifeView", B_FOLLOW_NONE, BGL_RGB | BGL_DEPTH | BGL_DOUBLE, 85 &fGLifeState); 86 87 pbvView->AddChild(fGLifeViewport); 88 89 return B_OK; 90 } 91 } 92 93 94 // ------------------------------------------------------ 95 // GLifeSaver Class StopSaver Definition 96 void 97 GLifeSaver::StopSaver(void) 98 { 99 if (fGLifeViewport != NULL) 100 fGLifeViewport->EnableDirectMode(false); 101 } 102 103 104 // ------------------------------------------------------ 105 // GLifeSaver Class DirectConnected Definition 106 void 107 GLifeSaver::DirectConnected(direct_buffer_info* pdbiInfo) 108 { 109 // Enable or disable direct rendering 110 #if 1 111 if (fGLifeViewport != NULL) { 112 fGLifeViewport->DirectConnected(pdbiInfo); 113 fGLifeViewport->EnableDirectMode(true); 114 } 115 #endif 116 } 117 118 119 // ------------------------------------------------------ 120 // GLifeSaver Class DirectDraw Definition 121 void 122 GLifeSaver::DirectDraw(int32 iFrame) 123 { 124 fGLifeViewport->Advance(); 125 } 126 127 128 // ------------------------------------------------------ 129 // Main Instantiation Function 130 extern "C" _EXPORT BScreenSaver* 131 instantiate_screen_saver(BMessage* pbmPrefs, image_id iidImage) 132 { 133 return new GLifeSaver(pbmPrefs, iidImage); 134 } 135